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 #