trying to sort rows in txt files

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 356
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 45
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

trying to sort rows in txt files

 
0
  #1
Sep 3rd, 2008
Does anyone remember how ms dos had a sort program that you could sort the rows in a huge text document based on just one character by its position from the left margin?

I am trying to recreate that effect in a notepad clone. I am sort of new to C# but not to programming, a friend of mine works as a repair man at a factory and the jobs his machines run are in text database files, that each new line is a row of data, one of the values is a number representing rather or not that row's data was ran and accepted, I would like to write a simple program sort function that would sort the rows by that one value so all the unaccepted rows would all go to the top, without affecting the text in the row. so that that text could be easily cut and pasted in a new document to be ran by the machines again.

sorry for the long back story,

I found some code that is suppose to read a file and sort it using a list view, but that's not exactly what I am going for, and I couldn't get it to work right.

I am willing to do whatever necessary but so far trial and error just hasn't done anything for me.

thanks,
DiamondDrake
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 356
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 45
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

I have something... not what I want

 
0
  #2
Sep 3rd, 2008
I have Gotten this far, I can put each line in order alphanumerically, but its the entire line, If anyone could point out how I could make the lines order alphanumerically by a specific character from the left margin, or at least point me in the right direction I would appreciate it. Thanks.


  1. List<string> myLines = new List<string>();
  2. using (StringReader r2 = new StringReader(txtMain.Text))
  3. {
  4. string txtLine;
  5. while ((txtLine = r2.ReadLine()) != null)
  6. {
  7. myLines.Add(txtLine);
  8. }
  9. }
  10. myLines.Sort();
  11. using (StringWriter sw = new StringWriter())
  12. {
  13. foreach (string s in myLines)
  14. {
  15. sw.WriteLine(s);
  16. }
  17. txtMain.Text = sw.ToString();
  18. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: trying to sort rows in txt files

 
0
  #3
Sep 4th, 2008
I dont see why you'd be having a problem the following

  1. List<String> lines = textBox1.Text.Split('\n').ToList();
  2. lines.Sort();
  3. textBox2.Text = String.Join("\n", lines.ToArray());

With

"a cat
a bat
a mat"

put into textbox1, sorted it correctly
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 356
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 45
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: trying to sort rows in txt files

 
0
  #4
Sep 4th, 2008
It does work, but it sorts by the entire line, Lets say I have a txt file like this:

110125614, 55614, 1289489 685481 685489 3 35478956
694894894, 85989, 7478498 651856 547878 1 68994898
547846514, 35247, 5248948 658748 848655 2 68489894
524754754, 32348, 5856445 489585 598788 0 91156419

I would like the sort function to ignore all the characters except one a specific number of characters from the margin for example the bold character in my example above so that the output would be:

524754754, 32348, 5856445 489585 598788 0 91156419
694894894, 85989, 7478498 651856 547878 1 68994898
547846514, 35247, 5248948 658748 848655 2 68489894
110125614, 55614, 1289489 685481 685489 3 35478956

see, I would like the lines not to change, just be ordered my a certain character in each line and I'm lost at how to go about it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: trying to sort rows in txt files

 
0
  #5
Sep 5th, 2008
As long as the lines are unique you would need to either use a dictionary and sort an array/list of those characters being the key to the dictionary, OR, you need to write your own sort routine.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 356
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 45
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: trying to sort rows in txt files

 
0
  #6
Sep 5th, 2008
i looked into using a dictionary, but I couldn't seem to taper its usage to match my intentions. and I don't know where to begin on writing my own sort routine. I am working on a method to compare substring values in an array containing all the lines. i will post some code when I have something that makes any sense at all.

any pointers or suggestions would be appreciated.
Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 356
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 45
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: trying to sort rows in txt files

 
0
  #7
Sep 5th, 2008
I found the answer,
basically I created 2 arrays, I read each line of text from my text box, copied it in its entirety into my first array, then I used a substring to find just the chosen column and copied that character to the 2nd array, then i called the Array.sort(array, array); method sorting the substring array, then wrote the array with the full lines to the text box not nice and sorted by the selected substing of each line, simple, Very efficient, and perfect.

Thanks for your suggestions LizR, but I guess I just needed to better investigate the usage of the methods I already knew about.

This thread is sloved.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: Schokbreker is an unknown quantity at this point 
Solved Threads: 0
Schokbreker Schokbreker is offline Offline
Newbie Poster

Re: trying to sort rows in txt files

 
0
  #8
Dec 11th, 2008
Is it possible for you to post the code you used?

I am struggling to do this as we speak, but can't get it to work.

Thanks in advance.

Schokbreker
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 356
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 45
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: trying to sort rows in txt files

 
0
  #9
Dec 11th, 2008
Originally Posted by Schokbreker View Post
Is it possible for you to post the code you used?

I am struggling to do this as we speak, but can't get it to work.

Thanks in advance.

Schokbreker
I found many ways to get the lines to the arrays, but the way that seemed to work the best for my application was to create some string lists add the values to the lists and then convert them to arrays sort the first using the 2nd as a key. then send them back to your text field or where ever you want them.

Its a TON of code that i worked very hard on, and it works great for 100,000 lines or so, but get over 250,000 lines and you tend to get memory errors.

I will give you a simple fly by version
  1. try
  2. {
  3. //create some string lists to hold your lines
  4.  
  5. List<string> mySortLines = new List<string>();
  6. List<string> mySortLines2 = new List<string>();
  7.  
  8. //declare a variable to hold the lengh of the first line
  9. int int4length = 0;
  10. int X = 0;
  11.  
  12. //create a string reader to read in the lines of text from RTB (txtMain)
  13. using (StringReader r3 = new StringReader(txtMain.Text))
  14. {
  15. //create some vars to hold the selected text (on the first line) to show what column to sort by.
  16.  
  17. string txtSortLine;
  18. int txtSelectedStart = txtMain.SelectionStart;
  19. int txtSelectedLength = txtMain.SelectionLength;
  20. string txtSortcharacter = string.Empty;
  21. //if no text is selected, abort and show error message
  22. if (txtSelectedLength == 0)
  23. {
  24. MessageBox.Show("you must select characters from the first line of text", "Sort Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  25. return;
  26. }
  27. //do the magic, create loop to run each line of text
  28. while ((txtSortLine = r3.ReadLine()) != null)
  29. {
  30.  
  31. //set int4lenght var
  32.  
  33. if (X < 1)
  34. {
  35. int4length = txtSortLine.Length;
  36. X++;
  37.  
  38. //check if text is selected on the first line
  39. if (int4length < txtSelectedStart)
  40. {
  41. MessageBox.Show("you must select characters from the first line of text", "Sort Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  42. return;
  43. }
  44. }
  45.  
  46. //check if line contains entire selected coulmns
  47. if (txtSortLine.Length >= (txtSelectedStart + txtSelectedLength))
  48. {
  49.  
  50. mySortLines.Add(txtSortLine);
  51. mySortLines2.Add(txtSortLine.Substring(txtSelectedStart, txtSelectedLength));
  52.  
  53.  
  54.  
  55. }
  56. //check if lines are shorter than selected columns if so sort as blank
  57. if (txtSelectedStart + 1 > txtSortLine.Length)
  58. {
  59. mySortLines.Add(txtSortLine);
  60. mySortLines2.Add("");
  61. ProgressBar.Value += 1;
  62. }
  63. //need to sort for lines that contain part of selected columns
  64. if (txtSortLine.Length >= (txtSelectedStart + 1) && txtSortLine.Length < (txtSelectedStart + txtSelectedLength))
  65. {
  66. mySortLines.Add(txtSortLine);
  67. mySortLines2.Add(txtSortLine.Substring(txtSelectedStart, txtSortLine.Length - (txtSelectedStart)));
  68. ProgressBar.Value += 1;
  69. }
  70.  
  71.  
  72. }
  73.  
  74. //convert lists to arrays, then sort lines, by column keys
  75. string[] sortArray = mySortLines.ToArray();
  76. string[] sortArray2 = mySortLines2.ToArray();
  77. Array.Sort(sortArray2, sortArray);
  78.  
  79. //little cleaning
  80. mySortLines = null;
  81. mySortLines2 = null;
  82. sortArray2 = null;
  83. //create a string writer and put the sorted text back to the RTB
  84. using (StringWriter sw2 = new StringWriter())
  85. {
  86. foreach (string s in sortArray)
  87. {
  88. sw2.WriteLine(s);
  89. }
  90. txtMain.Text = sw2.ToString();
  91. txtMain.Modified = true;
  92.  
  93.  
  94. sortArray = null;
  95.  
  96.  
  97. }
  98.  
  99.  
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. MessageBox.Show(ex.Message.ToString(), "Error");
  105. }
  106. }

that should be all the magic right there, took me weeks of asking all the wrong questions on 4 forums before I finally found while searching the usage of sorting arrays by a key. Noone could help me, I finally just figured it out. Its sloppy I know, but it works, there were no programs for windows that does this, I had to be the man to fix that. I have an application almost ready for release called sort pad it is a notepad like app that has tons of sorting functions, this being the most unique one.

Use it wisely,

DiamondDrake
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1988 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC