Sorting a Collection of Customized Objects
Sep 10, 2009

When using our own Object model in our projects, we have our own classes for user defined objects and collection of these objects to represent the list of these objects. For example we have an object named Customer and we have a customer collection object. We can directly bind this collection of customer to a DataGrid or GridView. But when we have to provide a sorting on columns in grid, the process we follow is a bit tedious. Here is a very simple way to sort our collection of any object without converting into DataTable or any other object.

We have a generic comparer class as follows:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Reflection;
/// <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">First Object</param>
/// <param name="y">Second Object</param>
/// <returns>Result of comparison</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 following is the code you need to put for sorting any collection of object. I have a collection of customers objects as

Customers[] objCustomers convert this object to a generic list i.e.
List<Customers> lstCustomers = new List<Customers>(objCustomers);

lstCustomers.Sort(new GeneriComparer<Customers>(sortColumnName,GenericComparer<Customers>.SortOrder.Ascending));
again take the sorted list back to your collection i.e. objCustomers = lstCustomers.ToArray();

This way you can customize sorting of your grid which is bound to a collection of objects. This is pretty fast compared to DataTable or DataView, where we need to convert and then sort.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Related Posts

How to get status message from Telerik MVC Upload Control

Download Telerik library from http://www.telerik.com/products/aspnet-mvc.aspx To see demo of uplod control, visit http://demos.telerik.com/aspnet-mvc/upload…

Jun 21, 2011

Setting up an Offshore Development Center: An advantage

Organizations big or small can now compete in the global market with the…

Aug 28, 2013

How to open large XML files

Problem: Open large xml files without loading the whole xml file in memory…

Jun 9, 2010
Top

Copyright © Prakash Software Solutions Pvt. Ltd. All Rights Reserved.