Sorting Collection of Custom Type using Generic

Introduction

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:

public interface IComparable
{
    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

public class Car : IComparable
{
    ...
    // IComparable implementation.
    int IComparable.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>
    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
}

Here in the above main method the cars contains a collection of Car object.

Now we pass the instance of GenericComparer class in the cars.Sort() method with the Sort Field and Sort Order passed in the constructor of the GenericComparer.


Similar Articles