Sorting Collection of Custom Type using Generic

This article describes the sorting techinque using Generic that can be implemented in any custom type collection based on the selected property.

For sorting any custom type the class must implement the System.IComparable interface which allow the object to be sorted based on some specified key.

Here is the definition:

publicinterfaceIComparable

{

int CompareTo(object o);

}

Now let us assume we have a Car class and if we want to sort the collection of car objects then we must implement IComparable interface and put the logic of how our car object is to be sorted in the CompareTo method

publicclassCar : IComparable

{

...

// IComparable implementation.

intIComparable.CompareTo(object obj)

{

Car temp = (Car)obj;

if(this.carID > temp.carID)

return 1;

if(this.carID < temp.carID)

return -1;

else

return 0;

}

}

As you can see, the logic behind CompareTo() is to test the incoming type against the current instance based on a specific point of data. The return value of CompareTo() is used to discover if this type is less than, greater than, or equal to the object it is being compared with (see below).

CompareTo()

Return Value Meaning

Any number less than zero

This instance comes before the specified object in the sort order.

Zero

This instance is equal to the specified object.

Any number greater than zero

This instance comes after the specified object in the sort order.

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>

publicsealedclassGenericComparer<T> : IComparer<T>

{

publicenumSortOrder { Ascending, Descending };

#region member variables

privatestring sortColumn;

privateSortOrder 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>

publicstring SortColumn

{

get { return sortColumn; }

}

///<summary>

/// Sorting order.

///</summary>

publicSortOrder 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>

publicint 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.