The Inquisitive Coder - Davy Brion’s Blog

Trying to walk that thin line between intelligence and ignorance

Easy and fast sorting of objects

Posted by Davy Brion on July 2nd, 2007

A coworker of mine recently pointed me to a blogpost by Marc Brooks which contains a fantastic library for sorting objects. The objects don’t have to implement an interface or inherit from a base class, it pretty much works with whatever you throw at it. And it’s very fast.

Suppose you have the following class:

public class MyClass
{
  private int _intValue;
  private double _doubleValue;
  private string _stringValue;
  private decimal _decimalValue;
  private DateTime _dateTimeValue;

  public int IntValue
  {
    get { return _intValue; }
    set { _intValue = value; }
  }

  public double DoubleValue
  {
    get { return _doubleValue; }
    set { _doubleValue = value; }
  }

  public string StringValue
  {
    get { return _stringValue; }
    set { _stringValue = value; }
  }

  public decimal DecimalValue
  {
    get { return _decimalValue; }
    set { _decimalValue = value; }
  }

  public DateTime DateTimeValue
  {
    get { return _dateTimeValue; }
    set { _dateTimeValue = value; }
  }
}

And suppose we have a method which creates a big list filled with instances of this class for us:

private static List<MyClass> CreateBigList()
{
  List<MyClass> list = new List<MyClass>();

  for (int a = 0; a < 10; a++)
  {
    for (int b = 0; b < 10; b++)
    {
      for (int c = 0; c < 10; c++)
      {
        for (int d = 0; d < 10; d++)
        {
          for (int e = 0; e < 10; e++)
          {
            MyClass myObject = new MyClass();
            myObject.IntValue = a;
            myObject.DoubleValue = b;
            myObject.StringValue = "string" + c;
            myObject.DecimalValue = d;
            myObject.DateTimeValue = DateTime.Now.AddSeconds(e);

            list.Add(myObject);
          }
        }
      }
    }
  }

  return list;
}

The list will contain 100,000 instances, ordered ascending.

Using Marc Brooks’ DynamicComparer, it’s incredibly easy to sort this list differently:

static void Main(string[] args)
{
  List<MyClass> list = CreateBigList();

  string sortExpression = "IntValue DESC, DoubleValue DESC, "
      + "StringValue DESC, DecimalValue DESC, DateTimeValue DESC";

  DynamicComparer<MyClass> dynamicComparer =
      new DynamicComparer<MyClass>(sortExpression);

  DateTime before = DateTime.Now;
  list.Sort(dynamicComparer.Comparer);
  DateTime after = DateTime.Now;

  Console.WriteLine("Elapsed Time: " + after.Subtract(before));
  Console.ReadLine();
}

Based on the sortExpression, the list will now be sorted in the reversed order. On my computer, the output of this code was:

Elapsed Time: 00:00:00.5908496

I’d say that’s pretty fast for sorting 100,000 instances on 5 properties. And incredibly easy.

Share/Save/Bookmark

3 Responses to “Easy and fast sorting of objects”

  1. den ben Says:

    I gave it a go myself and got this output with your code:

    Elapsed Time: 00:00:00.3104464

    It got me thinking though… I don’t know much about search algoritms but I do know that your generated list is very simple to sort desc (as in reverse-and-you’re-done-simple) so I figured I’d throw in some randomnessness (is that a word? ;-)
    Turns out that went even faster! lol… kinda blew my point though ;-)

  2. Davy Brion Says:

    i believe the correct term is simply ‘randomness’ :P
    still, regardless of the example i used, this is a great library… makes it really easy to sort whatever collection of business entities you need and because it generates the IL that does the sorting, it’s much faster than using reflection.

  3. Marc Brooks Says:

    Glad you guys like it… by the way, it’s no included in my Dynamic Reflection Library on CodePlex http://www.codeplex.com/Dynamic

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>