Tuesday, July 24, 2007

Generic List Sorting

I have a custom entity that I have created within my web application and I am binding it to my DataGrid (I know I should be using a GridView, but that change will come later...). Previously the data was being retrieved into a DataTable, so when I needed to sort, I could just apply a DataView to the DataTable passing the appropriate sort filter and I was set. But with the Generic list, it was not that simple. I did a little research on Delegates and Predicates to maybe solve my problem, but then I was creating custom code for each and every list that I needed to perform this on. However, in my Google searching I discovered this article, Sorting GridView Using IComparer by TheDotNetGuy and he is a using a GenericComparer class to do exactly what I wanted. I really liked the flexibility that this solution provided.

using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Reflection;

public class GenericComparer<T> : IComparer<T>
{
    private SortDirection _sortDirection;
    private string _sortExpression;

    /// <summary>
    /// Direction in which to sort.
    /// </summary>
    public SortDirection SortDirection
    {
        get { return this._sortDirection; }
        set { this._sortDirection = value; }
    }

    public GenericComparer(string sortExpression, SortDirection sortDirection)
    {
        this._sortExpression = sortExpression;
        this._sortDirection = sortDirection;
    }

    public int Compare(T x, T y)
    {
        PropertyInfo propertyInfo = typeof(T).GetProperty(_sortExpression);
        IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
        IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);

        if (SortDirection == SortDirection.Ascending)
        {
            return obj1.CompareTo(obj2);
        }
        else return obj2.CompareTo(obj1);
    }
}

As an example, I could sort a list of personnel by last name as follows:
    List<MyApp.Entities.Personnel> personnel = MyApp.Entities.Personnel.Load();
    personnel.Sort(new GenericComparer<MyApp.Entities.Personnel>("LastName",SortDirection.Ascending);

5 comments:

Anonymous said...

Works great - thanks for the post!

Saiba Samurai said...

Works great! Any ideas on sorting by more than one field at a time?

Such As Last Name then First Name?

Paige Cook said...

Saiba,

I would suggest that you leverage the power of LINQ and do an OrderBy expression followed with a ThenBy expression. Please see http://msdn.microsoft.com/en-us/library/bb534743.aspx for more information.

Or if you cannot leverage LINQ, then try the recursive example shown here: http://www.rosscode.com/blog/index.php?title=generic_multiple_sorting_for_typed_colle&more=1&c=1&tb=1&pb=1

Hope that helps.

Manthan said...

Thanks you so much ! working perfectly. Searching a lot and finally got the working version . :-)

Anonymous said...

Bind gridview to generic list and sorting in C#.