Skip to main content

Posts

Showing posts from June, 2009

DotNet: Generic Sorting Class

Recently, I was searching for a generic sorting approach on dotnet List. Many articles (Like one, two..etc) were suggesting standard way of creating IComparer implementer class and using that with generic list (List <T>) in our application. Writing IComparer class for every business object not only take huge time also create challenges in code management when it grow considerably. So, here is simple and effective generic sorting class which can be used with any custom business object and can be sorted on any one of its properties.

All you have to do is, copy paste the complete code in to a new .cs file.

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Srushti.Business
{
public class GenericComparer<T> : IComparer<T>
{
string _propName = string.Empty;
bool _sortDirection = true;
public GenericComparer(string propertyName, bool SortAscending)
{
_propName = propertyName;
_sortDirection = SortAscending;
}