Skip navigation

It’s been awhile since I did a brain dump but I promise todays dump will be interesting and exciting.

One of the new things that I have been working with at work is the strongly typed generic List<T> this class functions much like an ArrayList except that you’r not forced to guess what is bundled inside.  It wasn’t long though before I encountered a situation where I wanted to sort this list like you do with an ArrayList.  The only problem was that the same sort methods do not work.

Doing some research on the web I ran across some posts that pointed to a really neat method which when implemented looks like this:

List<CoffeeMaker> coffeeMakers = GetCoffeeMakers();
coffeeMakers.Sort(CoffeeMaker.SortByName);

This looks alot like how you sort an arraylist however the way we had been previously doing it at work would result in a sort looking something like this:

ArrayList coffeeMakers= GetCoffeeMakers();

coffeeMakers.Sort(new rootNamespace.subNamespace.Sorts.coffeeMakers());

There are two inherent issues with this first we don’t really know what objects are inside the coffeeMakers ArrayList and secondly our sort is maintained in a separate class file than our actual object.  But really these two objects the sort methods and the class itself are coupled and there is no need to separate them.  It’s also very easy to remember where your sorts are if you could say put them in the class themselves.

Well the Comparison<T> delegate provides a great way to do this.  Here is how we can get the result posted first above and also keep the code in the file.

Let’s build the class above….

class CoffeeMaker
{

[...]

public int ID;
public string Name;
public decimal Price;

}

The first thing we need to do is make the class inherit from the IComparable<T> baseclass.  While we are doing this we need to implement a CompareTo method otherwise we will get a compiler error form inheriting the IComparable class.  We might as well make this sort on the Name.  This is teh method which will be used by default if you call .Sort() without passing any parameters.

class CoffeeMaker : IComparable<CoffeeMaker>
{

[...]

public int ID;
public string Name;
public decimal Price;

[...]

public int CompareTo(CoffeeMaker other)
{

return  this.Name.CompareTo(other.Name);

}

}

Okay thats fine and dandy but how do we sort by other properties how do we make that handy dandy looking sorting in the very first code snippet.  We are going to define static Comparison<T> inside our object class.  See Below.

class CoffeeMaker : IComparable<CoffeeMaker>
{

public static Comparison<CoffeeMaker> SortByID =

delegate(CoffeeMaker cm1, CoffeeMaker c2)
{
return cm1.ID.CompareTo(cm2.ID);
};

[...]

public int ID;
public string Name;
public decimal Price;

[...]

public int CompareTo(CoffeeMaker other)
{

return  this.Name.CompareTo(other.Name);

}

}

This is all still kind of new to me so there may be greater complexity to this than first meets the eye but so far it seems simple its clean and it solves the design problem I have encountered of needing to sort a generic List<T>.

Advertisement

One Trackback/Pingback

  1. By LINQ and OrderBy() « Personal Blog of Sonickat on 28 Oct 2011 at 10:56 am

    [...] Up until recently when I wanted to sort a generic list of objects I used the methods I outlined back in June of 2011. [...]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.