Sort a list of C # string arrays

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 do I sort this list so that it is sorted 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 this is not the correct sorting of the second column. In addition to sorting by the first two columns in alphabetical order, how could I add to the third column?

+7
sorting arrays list c #
source share
3 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 
+9
source share

You can do:

 var newList = list.OrderBy(r => r[0]) .ThenBy(r => r[1]) .ThenBy(r => r[2]) .ToList(); 

Suppose your List has an element in a string array of at least 3 elements in length. First, a list will be sorted based on the first element of the array, Animal , then Bread and then Name .

If your List is defined as:

 List<string[]> list = 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"} }; 

A better approach to this problem is to create your own class with Type , Bread and Name as a property, and then use string[] instead

You can define your own class:

 public class Animal { public string Type { get; set; } public string Bread { get; set; } public string Name { get; set; } public Animal(string Type, string Bread, string Name) { this.Type = Type; this.Bread = Bread; this.Name = Name; } } 

and then define your List<Animal> as:

 List<Animal> animalList = new List<Animal> { new Animal("Dog", "Golden Retriever", "Rex"), new Animal("Cat", "Tabby", "Boblawblah"), new Animal("Fish", "Clown", "Nemo"), new Animal("Dog", "Pug", "Daisy"), new Animal("Cat", "Siemese", "Wednesday"), new Animal("Fish", "Gold", "Alaska"), }; 

Later you can get a sorted list, for example:

 List<Animal> sortedList = animalList .OrderBy(r => r.Type) .ThenBy(r => r.Bread) .ToList(); 

If you want, you can implement your own sorting, see How to use the IComparable and IComparer interfaces in Visual C #

+5
source share

If you can use LINQ, you can do something like:

 myanimals = myanimals.OrderBy(a => a[0]).ThenBy(a => a[1]).ToList(); 

Or the same in the request:

 myanimals = (from a in animals order by a[0], a[1] select a).ToList(); 
0
source share

All Articles