<$BlogRSDURL$>
My Blog Links
Site Links
Currently Reading
Blogroll
Archives
Tools
  • This page is powered by Blogger. Isn't yours?
  • Weblog Commenting and Trackback by HaloScan.com
  • 2RSS.com :: RSS directory
  • Listed on BlogShares
  • Blog Directory, Find A Blog, Submit A Blog, Search For The Best Blogs

Wednesday, July 28, 2004

.NET: Descending Comparer 

I was recently working on some code, and came upon an instance where I needed to sort an array of integers in descending order.  I looked at the Array.Sort method, but that orders them in ascending order by default. So, I started looking for an IComparer implementation that would sort in descending order. Amazingly enough, it doesn't appear that the .NET Framework has a comparer to do that (at least not in v1 or v1.1 -- not sure about v2) .

So, I decided to code one up myself.  It's actually a relatively simple concept but very useful. I created a DescendingComparer class that implements the IComparer interface.  In its Compare method, I just take the result of the default Comparer class in the .NET Framework and multiple it by -1.  This causes the array to be sorted in descending order.  So, here's the code:
/// <summary>

/// DescendingComparer is a comparer class the orders items in descending order.
/// </summary>
public sealed class DescendingComparer : IComparer
{
#region private members
private CultureInfo culture;
private static DescendingComparer defaultCurrent =
new DescendingComparer(CultureInfo.CurrentCulture);
private static DescendingComparer defaultInvariant =
new DescendingComparer(CultureInfo.InvariantCulture);
#endregion
 
#region public properties
/// <summary>
/// Gets the default DescendingComparer with its culture set to the CurrentCulture.
/// </summary>
public static DescendingComparer Default
{
get { return defaultCurrent; }
}
 
/// <summary>
/// Gets the default DescendingComparer with its culture set to the InvariantCulture.
/// </summary>
public static DescendingComparer DefaultInvariant
{
get { return defaultInvariant; }
}
#endregion
 
/// <summary>
/// Constructor that sets the CultureInfo for this class.
/// </summary>
/// <param name="culture">Culture for this comparer.</param>
public DescendingComparer(CultureInfo culture)
{
this.culture = culture;
}
 
/// <summary>
/// Performs a case-sensitive comparison of two objects of the same type and
/// returns a value indicating whether one is less than, equal to or greater
/// than the other.
/// </summary>
/// <param name="a">First object to compare.</param>
/// <param name="b">Second object to compare.</param>
/// <returns>Negative number means a is greater than b; 0 means they're equal;
/// Positive number means a is less than b.</returns>
public int Compare(object a, object b)
{
Comparer comparer = (culture == CultureInfo.InvariantCulture) ?
Comparer.DefaultInvariant : Comparer.Default;
return comparer.Compare(a, b) * -1;
}
}
Then, I use it by calling:
	Array.Sort(myArray, DescendingComparer.DefaultInvariant); 

And yeah, I know I could have just sorted the array and called Array.Reverse to get it in the right order, but that just seemed like a lot of needless operations when this can be done more efficiently with the right comparer class.