views:

79

answers:

2

There's no Sort() function for IList. Can someoene help me with this? I want to sort my own IList.

Suppose this is my IList:

public class MyObject() 
{
 public int number { get; set; }
 public string marker { get; set; }
}

How do I sort myobj using the marker string?

public void SortObject()
{
 IList<MyObject> myobj = new List<MyObject>();
}
+4  A: 

Use OrderBy

Example

public class MyObject() 
{
    public int number { get; set; }
    public string marker { get; set; }
}

IList<MyObject> myobj = new List<MyObject>();
var orderedList = myobj.OrderBy(x => x.marker).ToList();

For a case insensitive you should use a IComparer

public class CaseInsensitiveComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
    }
}

IList<MyObject> myobj = new List<MyObject>();
var orderedList = myobj.OrderBy(x => x.marker, new CaseInsensitiveComparer()).ToList();
BrunoLM
Thanks @Bruno this helps.
Rye
+1 for considering case-sensitivity, but the Framework already has case-insensitive string comparers, no need to create one. Try StringComparer.OrdinalIgnoreCase instead of your CaseInsensitiveComparer.
Joe
A: 
var sorted = myObj.OrderBy(x => x.marker);
Matthew Manela