I recently had to implement custom sorting on couple of ASP.net pages that had to bind to a collection of custom data objects. Not wanting to re-invent the wheel on every page, I looked for an easier solution online. I ran across at least two worth mentioning: Generic Sorting Using Reflection in .NET By Jon Wojtowicz (http://www.eggheadcafe.com/articles/pfc/propertycomparer.asp) and Rocky Lhotka's MSDN article where he presents his Generics based PropertyComparer. I liked Jon's solution in that it was written using recursion so that you could easily sort by properties of properties. However Jon's implementation was not "Generics" based. Rocky's implementation was generics based, but it lacked the recursion that my solution needed. In the end, I ended up modifying Jon's code to be generics based. I sent Jon an email asking for permission to share his modified code online, but unfortunately he never responded to my email.
Below is a link to my patch file to Jon's PropertyComparer.cs file. You can download the original directly from Jon's project hosted on EggHeadCafe in the link above.
PropertyComparer.patch (2.99 kb)
Here is an example of how you would use this to support sorting in a Telerik RadGrid:
protected void RadGrid1_SortCommand(object source, GridSortCommandEventArgs e)
List<Employee> employees = (List<Employee>)RadGrid1.DataSource;
GridSortExpression expression = new GridSortExpression();
cmpOrder = CompareOrder.Ascending;
expression.SortOrder = GridSortOrder.Ascending;
cmpOrder = CompareOrder.Descending;
expression.SortOrder = GridSortOrder.Descending;
employees.Sort(new PropertyComparer<Employee>(e.SortExpression, cmpOrder));
expression.FieldName = e.SortExpression;
e.Canceled = true;