Now if suppose we have more object in our application for example bus, two wheeler etc. then for each object we have to implement the IComparable interface in all the classes and implement the CompareTo() method.
The ideal solution is to take advantage of the Generic and developed a Generic Comparable class which can be used by any of the business object in our system.
Here is the code for doing it.
#region using directives
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Reflection;
#endregion
namespace com.mycompany.Common
{
/// <summary>
/// This class is used to compare any
/// type(property) of a class for sorting.
/// This class automatically fetches the
/// type of the property and compares.
/// </summary>
public sealed class GenericComparer<T> : IComparer<T>
{
public enum SortOrder { Ascending, Descending };
#region member variables
private string sortColumn;
private SortOrder sortingOrder;
#endregion
#region constructor
public GenericComparer(string sortColumn, SortOrder sortingOrder)
{
this.sortColumn = sortColumn;
this.sortingOrder = sortingOrder;
}
#endregion
#region public property
/// <summary>
/// Column Name(public property of the class) to be sorted.
/// </summary>
public string SortColumn
{
get { return sortColumn; }
}
/// <summary>
/// Sorting order.
/// </summary>
public SortOrder SortingOrder
{
get { return sortingOrder; }
}
#endregion
#region public methods
/// <summary>
/// Compare interface implementation
/// </summary>
/// <param name="x">custom Object</param>
/// <param name="y">custom Object</param>
/// <returns>int</returns>
public int Compare(T x, T y)
{
PropertyInfo propertyInfo = typeof(T).GetProperty(sortColumn);
IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);
if (sortingOrder == SortOrder.Ascending)
{
return (obj1.CompareTo(obj2));
}
else
{
return (obj2.CompareTo(obj1));
}
}
#endregion
}
}
Now suppose we have a Car class with property CarID and CarName then the below code demonstrate the sorting using GenericComparer class describe above.
static void Main(string[] args)
{
List<Car> cars = new List<Car>();
cars.Add(new Car("1", "Maruti 800"));
cars.Add(new Car("2", "Honda City"));
.....
.....
cars.Sort(new GenericComparer<Car>("CarName", GenericComparer<Car>.SortOrder.Ascending));
.....
.....
//display here