One of the popular generic collection types in .Net 2.0 is List<T>, which we use to store a list of T items. For example, List<Country> stores a list of countries that defined by Country class. It works perfectly to bind the collection to a datasource to be displayed on, for instance drop down or grid view. But you may want the list to be sorted before it is bound to the datasource. Yes, List collection support a method called List<T>.sort() but in order to use sort() method, T must inherit IComparable interface.
To read more about this, visit here.
So let's take Country as the example. A typical Business Entity will look like:-
public partial class Country
{
public Country()
{
}
public Country(System.String countryName, System.Int32 id)
{
this.countryNameField = countryName;
this.idField = id;
}
private System.String countryNameField;
public System.String CountryName
{
get { return this.countryNameField; }
set { this.countryNameField = value; }
}
private System.Int32 idField;
public System.Int32 Id
{
get { return this.idField; }
set { this.idField = value; }
}
}
Next step is to inherit IComparable interface.
public partial class Country: IComparable<Country>
Once you have inherit the Interface, you are required to implement the definitions. In C#, you can right click on the Interface, and select Implement Interface > Implement Interface. Then the required method will be automatically added to your class. It will look similar to this:-
#region IComparable<Country> Members
public int CompareTo(Country other)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
Alright, you are almost done with a simple sorting. Now you need to implement the CompareTo method like this:-
#region IComparable<Country> Members
public int CompareTo(Country other)
{
return CountryName.CompareTo(other.CountryName);
}
#endregion
To test it, simply fill the List<Country> with data and call List<Country>.sort() method. It will automatically sort the list. Now is this what we need? If yes, move on to others. If no, what is the problem? Come to think of it, we may have different comparison in different scenario. For example, we may want to sort by ID in a GridView, and we may want to sort by Name (ASC or DESC) in a drop down box. So we want a more dynamic implementation.
Here, we need to use Generic Comparison delegate to provide different comparison methods. We may want to sort by ID or sort by Name in Descending order. Alright, you may simple need to write this:
#region Comparison Function
public static Comparison<Country> SortById = delegate(Country c1, Country c2)
{
return c1.Id.CompareTo(c2.Id);
};
// Sort by Name in DESC order
public static Comparison<Country> SortByName_DESC = delegate(Country c1, Country c2)
{
return c2.CountryName.CompareTo(c1.CountryName);
};
#endregion
So with Comparison delegates, you can come out with different kind of comparison to help you sort the list in your own way. The methods are being called in this way:
List<Country> list = new List<Country>();
list = GetCountries();
// Default Sorting
list.sort();
//Sort By ID
list.sort(Country.SortById);
//Sort By Name DESC
list.sort(Country.SortByName_DESC);
Done! If the above example does not help to solve your problem, you may want to consider creating a custom comparer class by inheriting from IComparer interface.
Cheers.