I need a basic example of how to use the IComparable
interface so that I can sort in ascending or descending order and by different fields of the object type I\'m s
If you want dynamic sort, you can use LINQ
var itemsOrderedByNumber = ( from item in GetClasses() orderby item.Number select item ).ToList();
var itemsOrderedByText = ( from item in GetClasses() orderby item.Text select item ).ToList();
var itemsOrderedByDate = ( from item in GetClasses() orderby item.Date select item ).ToList();
or "Sort" method of List class:
List itemsOrderedByNumber2 = new List( GetClasses() );
itemsOrderedByNumber2.Sort( ( a, b ) => Comparer.Default.Compare( a.Number, b.Number ) );
List itemsOrderedByText2 = new List( GetClasses() );
itemsOrderedByText2.Sort( ( a, b ) => Comparer.Default.Compare( a.Text, b.Text ) );
List itemsOrderedByDate2 = new List( GetClasses() );
itemsOrderedByDate2.Sort( ( a, b ) => Comparer.Default.Compare( a.Date, b.Date ) );