Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort list of string arrays c#

I have a list of string arrays, where the arrays are formatted as [Animal, Breed, Name]:

{ ["Dog", "Golden Retriever", "Rex"],
  ["Cat", "Tabby", "Boblawblah"],
  ["Fish", "Clown", "Nemo"],
  ["Dog", "Pug", "Daisy"],
  ["Cat", "Siemese", "Wednesday"],
  ["Fish", "Gold", "Alaska"]
}

How would I sort this list so that it was arranged alphabetically by "Animal", and then "Breed"? i.e.:

{ ["Cat", "Siamese", "Boblawblah"],
  ["Cat", "Tabby", "Wednesday"],
  ["Dog", "Golden Retriever", "Rex"],
  ["Dog", "Pug", "Daisy"],
  ["Fish", "Clown", "Nemo"],
  ["Fish", "Gold", "Alaska"]
}

I am currently trying:

animalList.Sort((s, t) => String.Compare(s[0], t[0]));

But that is not sorting the second column correctly. In addition to sorting by the first two columns alphabetically, how would I then add in the third column?

like image 925
suhMAN Avatar asked May 26 '14 15:05

suhMAN


People also ask

How do you sort an array of strings?

To sort a String array in Java, you need to compare each element of the array to all the remaining elements, if the result is greater than 0, swap them.

Can we sort string in C?

C Sort String in Ascending Order Receive any string using gets() function. Get the length of string using strlen() function of string. h library and initialize it to any variable say len. Now create a for loop that runs from 0 to one less than the string length.

Can we sort char array in C?

Below is the simple C program that will accept the character string from the user, calculates the length of the string and performs sorting operation on the string. The program will sort the character array in ascending order and display the result string (character array).

Can selection sort be used for strings?

Sort an Array of string using Selection sort in C++ In each emphasis of determination sort, the base component from the unsorted subarray is picked and moved to the arranged subarray.


1 Answers

You can use LINQ:

animalList = animalList
    .OrderBy(arr => arr[0])
    .ThenBy(arr  => arr[1])
    .ToList();

Your sample:

List<string[]> animalList = new List<String[]>{ 
            new []{"Dog", "Golden Retriever", "Rex"},
            new []{"Cat", "Tabby", "Boblawblah"},
            new []{"Fish", "Clown", "Nemo"},
            new []{"Dog", "Pug", "Daisy"},
            new []{"Cat", "Siemese", "Wednesday"},
            new []{"Fish", "Gold", "Alaska"}
        };

Result:

-       [0] {string[3]} string[]
        [0] "Cat"   string
        [1] "Siemese"   string
        [2] "Wednesday" string
-       [1] {string[3]} string[]
        [0] "Cat"   string
        [1] "Tabby" string
        [2] "Boblawblah"    string
-       [2] {string[3]} string[]
        [0] "Dog"   string
        [1] "Golden Retriever"  string
        [2] "Rex"   string
-       [3] {string[3]} string[]
        [0] "Dog"   string
        [1] "Pug"   string
        [2] "Daisy" string
-       [4] {string[3]} string[]
        [0] "Fish"  string
        [1] "Clown" string
        [2] "Nemo"  string
-       [5] {string[3]} string[]
        [0] "Fish"  string
        [1] "Gold"  string
        [2] "Alaska"    string
like image 73
Tim Schmelter Avatar answered Oct 08 '22 00:10

Tim Schmelter