I have a list of type <string[]>, and each item in the list is a one-dimensional array. I need to sort the list according to a particular field in the arrays, how can I do this? An illustration for clarity:

list item #1 = array(112, Maxwell Smart, MIS)
list item #2 = array(208, Bubba Joe, BNJ)
list item #3 = array(407, Mary Poppins, MEP)

I need the list re-ordered so that the arrays are sorted by the last field of the array, so the list should end up being like this:
list item #1 = array(208, Bubba Joe, BNJ)
list item #2 = array(407, Mary Poppins, MEP)
list item #3 = array(112, Maxwell Smart, MIS)

By providing an IComparer

Would these not be better served as custom objects instead of arrays?

list item #1 = array(112, Maxwell Smart, MIS)
list item #2 = array(208, Bubba Joe, BNJ)
list item #3 = array(407, Mary Poppins, MEP)

Use Linq, and its Extension methods:
Example:
you add:

List<string[]> list = new List<string[]>();
list.Add(new string[] {"112", "Maxwell Smart", "MIS"});

and others...
now you would like to sort by last index of array - this are data MIS, BNJ and MEP (if Im correct).

---
The best way would be to create a new class and create 3 propertis inside of it (number, name and abbreviation, then fill it with the data from your array (or you can do it directly, so you wont have 2 lists.
This is how you can do:

class Person
{
    public int Number {get;set;}
    public string Name {get;set;}
    public string Abbreviation {get;set;}
}

Now create a list and fill it up from your current list:

List<Person> list2 = new List<Person>();
foreach(string[] data in list)
{
    list2.Add(new Person{Number = int.Parse(data[0]), Name = data[1], Abbreviation = data[2]});
}

now you can sort it:

var query = list2.OrderBy(o => o.Abbreviation).ToList();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.