Home > .NET > Generic Sorting and poor late bound performance – a solution?

Generic Sorting and poor late bound performance – a solution?

What a month! Relocated the family to Washington DC, fought with the DC DMV, finally closed on the first house we have ever sold and got the flu!

In case, you missed it – I wrote this article on Generic Sorting on AspAlliance recently. Itdemonstrates a technique I use frequently in applications and I was really surprised to see the incredibly poor performance that late binding introduces when used over and over again in something like a sorting algorithm. This led me to experiment with caching the late bound results (something I have never done in any of the production applications that use this technique) – I was mildly satisfied with the caching results and consoled myself with the idea that performance in sorting within your applications is probably not a big issue unless it is used frequently and your application is under really heavy load.

Enter Keith J. Farmer who read my article and suggested theconcept used by regular expressions – compile the sorters! What an interesting idea!Instead of doing all the comparisons using late binding – the developer would specify the sorting logic to use (e.g. by the “Name” property descending and “Age” property ascending) and then request that the “factory” create a sorter. This would mean that the factory would also have to know the type of the instances it will be sorting (the type that defines the properties for sorting) and then would have to build the class from code, compile it (CODEDOM?) and then create an instance and return it.

The use case for the code might be something like:

PropertyComparerFactory factory = new PropertyComparerFactory(typeof(Person));

factory.SortByProperty(“Name“, CompareOrder.Ascending);

factory.SortByProperty(“Age“, CompareOrder.Descending);

PropertyComparer comparer = factory.CreateInstance();

Arrays.Sort(people, comparer);

Neither Keith nor I have tried this idea out yet with real code but I hope to do so soon. 😀

Comments, ideas anddiscussion are welcome as always.

Categories: .NET
  1. eichert12@hotmail.com (Steve)
    June 30, 2004 at 12:51 pm

    Sounds like an excellent idea. I’ve done something similar with Filters for my collections. Rather then looping over all the items in a collection and comparing a single property to the all important value I have a factory method create the “Collection Finder.” Similar to what your talking about and works great!

  1. No trackbacks yet.

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 )

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

%d bloggers like this: