Sorting a List Object

Saturday, August 9, 2008


When Generics came out in Visual Studio 2005, the new List object was introduced.  This feature was a new collection object that also gave you the ability to sort records based on whatever criteria you wanted.  There was also a new coding feature that came out called “anonymous functions” which basically allowed you to plop a blob of code in a parameter of another function.   The Sort function was a big step in sorting collections, much simpler than in Visual Studio 2003 but it still a bit jankie.

Sorting in Visual Studio 2005

So let's take a look at how to do this old way and then look at some new ways we can do the same thing in Visual Studio 2008.

   30         public enum SortDirection

   31         {

   32             Ascending = 1,

   33             Descending = -1

   34         }



   37         public List<GuestDto> SortLastNameUsingAnonymousFunction()

   38         {

   39             if (guests == null)

   40                 return null;


   42             guests.Sort(new Comparison<GuestDto>(delegate(GuestDto guest1, GuestDto guest2)

   43                 {

   44                     return Convert.ToInt32(SortDirection.Ascending) * guest1.LastName.CompareTo(guest2.LastName);

   45                 }));


   47             return guests;

   48         }

  Even after having done this many times, I still need to refer to an example before doing it because it is just not that intuitive.

Using Lambda Expressions in the Same Example

Now let's look at a cleaner way to do this that even I can remember.

   50         public List<GuestDto> SortLastNameUsingLambdaExpressions()

   51         {

   52             if (guests == null)

   53                 return null;


   55             guests.Sort((guest1, guest2) =>

   56                 Convert.ToInt32(SortDirection.Ascending) * guest1.LastName.CompareTo(guest2.LastName));


   58             return guests;

   59         }

Essentially the way it works is the parameters are placed on the left side of the => statement and the body of the code you want to execute is on the right.   All the types (including guest1, guest2, the new Comparison object , and the delegate and return statements) are automatically determined by the compiler so you do not need to specify it.  So the Lambda Expression is a nice way to abbreviate anonymous methods and make the code more readable.  

 Adding Linq to the Equasion

When I mentioned to my team the neat way sorting which is easier to code, a co-worker and friend Mike Bosch (great blog here) showed me an even better approach by just referencing the Linq namespace.


   62         public List<GuestDto> SortLastNameUsingLinqLambdaExpressions()

   63         {

   64             if (guests == null)

   65                 return null;


   67             List<GuestDto> sortedGuests = guests.OrderBy(g => g.LastName).ToList();


   69             return sortedGuests;


   71         }

To which in the immortal words of Bill and Ted's Excellent Adventure, I say "PARTY ON DUDES!"



comments powered by Disqus