Sorting The Easy Way in C#

If you have a list of objects, say List<Widget> it’s mighty easy to to sort them using .NET.

First, derive the Widget class from IComparable:

public class Widget : IComparable

Then create the CompareTo method. In this case, we’re sorting the list of Widgets by name:

public int CompareTo(object obj)
if( !(obj is SynthProfile ))
return 0; // We could throw an exception here if we wanted to.
return this.Name.CompareTo(((SynthProfile)obj).Name);

Once that’s done all you have to do is call: