Thursday, January 22, 2009

Use delegate to sort the generic list (collection)

Here is an example of how to use the delegate to sort the list:

returnList.Sort(delegate(IMyClass x, IMyClass y)
return String.Compare(x.Name, y.Name);

So, instead of override the ICompare, you can use this short-cut to return a sorted list.

Here is how to just make your Thing class implement IComparable, implementing the CompareTo method like this:

public int CompareTo(Thing other)
return Name.CompareTo(other.Name);

No comments: