Fun with Generics – Simple sorting, pre-Linq

I recently attended the IowaCodeCamp.  I’ve found that going to these kinds of things always gets my brain buzzing with all kinds of things I want to try out.  In one talk I attended the presenter made the comment that most people have very little experience with Generics outside of using Generic Collections.  From what I’m seeing out there on the web, I would have to agree.  I’ve been playing a lot with Generic functions and classes recently and wanted to post a fun example I’ve come up with.  If this is useful to you, please feel free to grab it and reuse it.
So I wrote this because I love how easy Linq makes some things, like sorting (Ordering).  However, where I’m working, we’re not on the 3.5 Framework yet, so I can’t take advantage of the cool OrderBy function with Lambdas.  However, being able to order a collection quickly is a super nice feature.  So I wrote this function.  Hope you like it.
private Comparison GetComparison(String fieldName)
	List properties = new List(typeof(T).GetProperties());

	PropertyInfo property = properties.Find(

	  delegate(PropertyInfo propertyInfo)
		  return propertyInfo.Name.Equals(fieldName);


	if (property != null)
		return delegate(T obj1, T obj2)
			IComparable value1 = (IComparable)property.GetValue(obj1, null);
			IComparable value2 = (IComparable)property.GetValue(obj2, null);
			return value1.CompareTo(value2);

	throw new ArgumentException(String.Format("Object {0} has no property called {1}",
		typeof(T).Name, fieldName));

This function can then be invoked like this:

private List GetSortedList()
    List people = new List
        new Person{ FirstName = "Jim", LastName = "Smith", Address = "100 C Street" },
        new Person{ FirstName = "Amy", LastName = "Jones", Address = "100 A Street" },
        new Person{ FirstName = "Larry", LastName = "Johnson", Address = "100 B Street"}


I still prefer using Linq, but when I can’t, this is a nice little helper function.  Using similar techniques, your own 2.0 replacements for nice Linq functions can be created pretty easily.


One thought on “Fun with Generics – Simple sorting, pre-Linq

  1. Awesome Nate… Got Tominski\’s tweet about this article… it\’s been very helpful. I started with VB3 and now on C# but have done a poor job the last 5 years or so of keeping up with the changes… In an era when you can search the web and find a snippet of code for just about anything you want to do, we get lulled into a false sense of knowing what we are doing, or why it works the way it does… Lambdas, linq, generics… I can use them to a degree, but really don\’t understand them and never think about utilzing them (with the exception of generics, i suppose) in my code in the natural course of development…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s