I often need to sort a generic list on some arbitrary property. After writing the code a couple of times I decided to make it more generic using generics and some reflection. Oh, I know that reflection if costly and this is not a good way to sort large lists, but I typically work with small lists.

?View Code CSHARP
public enum SortDirection
{
   Ascending, Descending
}
 
public class ListSorter where T : class
{
  public static List Sort
(
              List<T> listToSort,
              string propertyName,
              SortDirection direction) where P : IComparable
  {
    Type propertyType = typeof (P);
    Type comparableInterface = propertyType.GetInterface("IComparable");
 
    if (comparableInterface == null)
        throw new Exception("Properties to sort by must be IComparable");
 
    listToSort.Sort(
        delegate(T x, T y)
            {
              PropertyInfo p1 = x.GetType().GetProperty(propertyName, propertyType);
              PropertyInfo p2 = y.GetType().GetProperty(propertyName, propertyType);
 
              object p1objvalue = p1.GetValue(x, null);
              object p2objvalue = p2.GetValue(y, null);
 
              P p1value = (P)p1objvalue;
              P p2value = (P)p2objvalue;
 
              if (direction == SortDirection.Ascending)
                  return p1value.CompareTo(p2value);
              else
                  return p2value.CompareTo(p1value);
          });
 
    return listToSort;
  }
}

To sort a list of Person objects by the string property FirstName do the following:

?View Code CSHARP
List<Person> myUnsortedList = GetPersonsInRandomOrder();
List<Person> sortedList =  ListSorter
                                     .Sort(
                                     myUnsortedList,
                                     "FirstName",
                                     SortDirection.Ascending);

To sort a list of Person objects by the int property Age do the following:

?View Code CSHARP
List<Person> myUnsortedList = GetPersonsInRandomOrder();
List<Person> sortedList =  ListSorter
                                     .Sort(
                                     myUnsortedList,
                                     "Age",
                                     SortDirection.Ascending);

If you have the luxury of using .NET Framework 3.0 or 3.5 you can use Linq to solve the problem really quick:

?View Code CSHARP
 
var sortedPersonList = from p in unsortedPersonList
                               orderby p.FirstName
                               select p;
 
foreach (var person in sortedPersonList)
{
    Console.WriteLine(person.FirstName + " " + person.LastName);
}

Leave a comment to this post if you have any thoughts.