Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

Using Visual Studio 2015.
I am reading a text file into an array. Text file data looks like this:

Cell 1, 435
Cell 1, 655
Cell 2, 923
Cell 1, 233
Cell 2, 222

Reading the text file works properly. The array has the data as it should be. I am just using the array.sort and it seems to be working and placing the data in the proper order.

I need to be able to sort the array on the Cell #.
Next I need to be able to take only a line with Cell 1 or Cell 2 in it and and display the whole line results into listboxes.

Thank you for any help

What I have tried:

I can only loop through the whole array and display it in the listbox. I am unable to select individual lines based on the Cell #
Posted
Updated 19-May-18 6:24am
Comments
Richard MacCutchan 19-May-18 12:11pm    
Does your array contain just the strings above, or are you splitting the lines into their separate fields? Doing the latter would mean you can easily combine fields for the same cell into a string and display the data that way.

1 solution

To sort on the cell number, you need to "explore" each line, and extract the cell number, convert it to a numeric value, and then sort the array by that. Sorting an array of strings sorts them using string comparison which compares two strings character by character, and the result of the entire comparison is based on the first difference.
So your sort order would be
Cell 1
Cell 10
Cell 11
...
Cell 19
Cell 2
...
The simplest way to do what you want is to write a static method which compares two strings and returns an integer:
C#
public static int CompareLines(string a, string b)
   {
   ...
   }
And use it using the Array.Sort override that accepts a delegate:
C#
Array.Sort(myArrayOfStrings, ComapareLines);

When you write the the method, it should return -1 if a is before b, 0 if they are the same, and 1 if b is before a.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900