Click here to Skip to main content
Click here to Skip to main content

Tagged as

A Generic Comparison Class for Collection Items

, 26 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
You probably have better things to do than writing tedious comparison methods.
I was looking around on the web for a method for sorting a list without having to write all the custom compare methods, and I came across this 2006 code on the by a guy named Dipend Lama on the c-sharpcorner website:

    Sorting Collection of Custom Type using Generic


After copying the code to my project, and seeing the results, I decided I occasionally needed to sort by more then just one property at a time. Unfortunately, this class didn't support that particular design consideration, so it was up to me to implement it.

My first task was to decide on a way to pass multiple (or even just one) properties on which to search. I settled on an array of strings because they're easy to instantiate in a method call. However, if just a single property was specified, I wanted to allow the programmer to do so without having to create a string array, so I kept the original constructor, and overloaded it with one that accepted a string array. The next problem was slightly tougher - wow was I going to sort the list with ALL of the properties?

My solution boiled down to a single basic programming practice - recursion. In all honesty, recursion is not all that common. I've written MILLIONS of lines of code in the last 30 years and have used recursion MAYBE half a dozen times.

It was actually very simple to do. I took the original Compare method:
 
public int Compare(T x, T y)
{
    PropertyInfo propertyInfo = typeof(T).GetProperty(sortColumn);
    IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
    IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);
    if (sortingOrder == SortOrder.Ascending)
    {
        return (obj1.CompareTo(obj2));
    }
    else
    {
        return (obj2.CompareTo(obj1));
    }
}

and changed it to this:
 
public int Compare(T x, T y)
{
    return CompareProperty(0, x, y);
}
 
private int CompareProperty(int index, T x, T y)
{
    int result = 0;
    PropertyInfo propertyInfo = typeof(T).GetProperty(propertyArray[index]);
    IComparable  obj1        = (IComparable)propertyInfo.GetValue(x, null);
    IComparable  obj2        = (IComparable)propertyInfo.GetValue(y, null);
    if (sortingOrder == GenericSortOrder.Ascending)
    {
        result = (obj1.CompareTo(obj2));
    }
    else
    {
        result = (obj2.CompareTo(obj1));
    }
    if (result == 0 && index < propertyArray.Length - 1)
    {
        index++;
        result = CompareProperty(index, x, y);
    }
    return result;
}

The new method calls itself for each property in the string array, and once the result of the compare is not 0, I know it's done, and can back out of the stack. Here's the whole thing for easy cut/pasting into your own code.
 
public enum GenericSortOrder { Ascending, Descending };
 
public sealed class GenericComparer<T> : IComparer<T>
{
    private GenericSortOrder sortingOrder;
    private string[] propertyArray = null;
 
<pre>
//------------------------------------------------------------------
public GenericSortOrder SortingOrder { get { return sortingOrder; } }
 
//------------------------------------------------------------------
public GenericComparer(string sortColumn, GenericSortOrder sortingOrder)
{
    if (string.IsNullOrEmpty(sortColumn))
    {
        throw new Exception("The sortColumn parameter is null/empty");
    }
    this.sortingOrder = sortingOrder;
    this.propertyArray = new string[1] {sortColumn};
}
 
//------------------------------------------------------------------
public GenericComparer(string[] properties, GenericSortOrder order)
{
    if (properties == null || properties.Length &lt; 1)
    {
        throw new Exception("Properties array cannot be null/empty.");
    }
    this.sortingOrder = order;
    this.propertyArray = properties;
}
 
//------------------------------------------------------------------
public int Compare(T x, T y)
{
    return CompareProperty(0, x, y);
}
 
//------------------------------------------------------------------
private int CompareProperty(int index, T x, T y)
{
    int result = 0;
    PropertyInfo propertyInfo = typeof(T).GetProperty(propertyArray[index]);
    IComparable  obj1        = (IComparable)propertyInfo.GetValue(x, null);
    IComparable  obj2        = (IComparable)propertyInfo.GetValue(y, null);
    if (sortingOrder == GenericSortOrder.Ascending)
    {
        result = (obj1.CompareTo(obj2));
    }
    else
    {
        result = (obj2.CompareTo(obj1));
    }
    if (result == 0 && index &lt; propertyArray.Length - 1)
    {
        index++;
        result = CompareProperty(index, x, y);
    }
    return result;
}

}
Usage looks something like this:
 
List<MyObject> list = new List<MyObject>();
 
list.Sort(new GenericCompare<MyObject>("singleProperty", GenericOrder.Descending);
 
list.Sort(new GenericCompare<MyObject>(new string[2] {"property1", "property2"}, 
          GenericSortOrder.Descending);

Care must be exercised that you don't sort on too many properties, or your code will throw a stack overflow exception.

There may be more elegant solutions out there, and if you know of one, by all means, post it as an alternative.

EDIT ===========================

And here's the compare method without recursion (many thanks to supercat9):
 
public int Compare(T x, T y)
{
    int          index  = 0;
    int          count  = propertyArray.Length;
    int          result = 0;
    PropertyInfo propertyInfo;
    IComparable  obj1;
    IComparable  obj2;
    do
    {
        propertyInfo = typeof(T).GetProperty(propertyArray[index]);
        obj1         = (IComparable)propertyInfo.GetValue(x, null);
        obj2         = (IComparable)propertyInfo.GetValue(y, null);
        result       = obj1.CompareTo(obj2);
        if (this.SortingOrder == GenericSortOrder.Descending)
        {
            result = -result;
        }
        index++;
    } while (result == 0 && index < count);
    return result;
}





EDIT (08/26/2010) -------------

Fixed some non-escaped <> brackets.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
Generalliked it. PinmemberHiren Solanki29-Aug-10 21:01 
GeneralReason for my vote of 5 Thank you for sharing this. Pinmemberlinuxjr26-Aug-10 9:56 
GeneralReason for my vote of 5 Thanks JSOP another one that is grea... PinmemberSimon_Whale26-Aug-10 5:57 
GeneralMixed Sort Directions Pinmemberdgauerke25-Jun-10 9:31 
GeneralNon-recursive method Pinmembersupercat94-Mar-10 12:10 
GeneralRe: Non-recursive method PinmemberJohn Simmons / outlaw programmer4-Mar-10 13:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150331.1 | Last Updated 26 Aug 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid