Microsoft KB Archive/320727

= How to use the IComparable and IComparer interfaces in Visual C# =

Article ID: 320727

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q320727



For a Microsoft Visual Basic .NET version of this article, see 322025.

IN THIS TASK
SUMMARY
 * IComparable
 * IComparer
 * Step-by-Step Example



SUMMARY
This step-by-step article describes the use of two interfaces: IComparer and IComparable. These interfaces are discussed in the same article for two reasons. These interfaces are frequently used together, and although the interfaces are similar (and have similar names), they serve different purposes.

If you have an array of types (such as string or integer) that already support IComparer, you can sort that array without providing any explicit reference to IComparer. In that case, the elements of the array are cast to the default implementation of IComparer (Comparer.Default) for you. However, if you want to provide sorting or comparison capability for your custom objects, you must implement either or both of these interfaces.

The following .NET Framework Class Library namespace is referenced in this article:

System.Collections

back to the top

IComparable
The role of IComparable is to provide a method of comparing two objects of a particular type. This is necessary if you want to provide any ordering capability for your object. Think of IComparable as providing a default sort order for your objects. For example, if you have an array of objects of your type, and you call the Sort method on that array, IComparable provides the comparison of objects during the sort. When you implement the IComparable interface, you must implement the CompareTo method, as follows: // Implement IComparable CompareTo method - provide default sort order. int IComparable.CompareTo(object obj) {  car c=(car)obj; return String.Compare(this.make,c.make);

} The comparison in the method is different depending on the data type of the value that is being compared. String.Compare is used in this example because the property that is chosen for the comparison is a string.

back to the top

IComparer
The role of IComparer is to provide additional comparison mechanisms. For example, you may want to provide ordering of your class on several fields or properties, ascending and descending order on the same field, or both.

Using IComparer is a two-step process. First, declare a class that implements IComparer, and then implement the Compare method: private class sortYearAscendingHelper : IComparer {  int IComparer.Compare(object a, object b)   { car c1=(car)a; car c2=(car)b; if (c1.year > c2.year) return 1; if (c1.year < c2.year) return -1; else return 0; } } Note that the IComparer.Compare method requires a tertiary comparison. 1, 0, or -1 is returned depending on whether one value is greater than, equal to, or less than the other. The sort order (ascending or descending) can be changed by switching the logical operators in this method.

The second step is to declare a method that returns an instance of your IComparer object: public static IComparer sortYearAscending {        return (IComparer) new sortYearAscendingHelper; } In this example, the object is used as the second argument when you call the overloaded Array.Sort method that accepts IComparer. The use of IComparer is not limited to arrays. It is accepted as an argument in a number of different collection and control classes.

back to the top

Step-by-Step Example
The following example demonstrates the use of these interfaces. To demonstrate IComparer and IComparable, a class named car is created. The car object has the make and year properties. An ascending sort for the make field is enabled through the IComparable interface, and a descending sort on the make field is enabled through the IComparer interface. Both ascending and descending sorts are provided for the year property through the use of IComparer.  In Visual C#, create a new Console Application project. Name the application ConsoleEnum.  Rename Class1.cs as Host.cs, and then replace the code with the following code.

Note In Visual Studio 2005, rename Program.cs as Host.cs. using System;

namespace ConsoleEnum {  class host {     [STAThread] static void Main(string[] args) {        // Create an arary of car objects. car[] arrayOfCars= new car[6] {           new car(&quot;Ford&quot;,1992), new car(&quot;Fiat&quot;,1988), new car(&quot;Buick&quot;,1932), new car(&quot;Ford&quot;,1932), new car(&quot;Dodge&quot;,1999), new car(&quot;Honda&quot;,1977) };        // Write out a header for the output. Console.WriteLine(&quot;Array - Unsorted\n&quot;);

foreach(car c in arrayOfCars) Console.WriteLine(c.Make + &quot;\t\t&quot; + c.Year); // Demo IComparable by sorting array with &quot;default&quot; sort order. Array.Sort(arrayOfCars); Console.WriteLine(&quot;\nArray - Sorted by Make (Ascending - IComparable)\n&quot;);

foreach(car c in arrayOfCars) Console.WriteLine(c.Make + &quot;\t\t&quot; + c.Year);

// Demo ascending sort of numeric value with IComparer. Array.Sort(arrayOfCars,car.sortYearAscending); Console.WriteLine(&quot;\nArray - Sorted by Year (Ascending - IComparer)\n&quot;);

foreach(car c in arrayOfCars) Console.WriteLine(c.Make + &quot;\t\t&quot; + c.Year);

// Demo descending sort of string value with IComparer. Array.Sort(arrayOfCars,car.sortMakeDescending); Console.WriteLine(&quot;\nArray - Sorted by Make (Descending - IComparer)\n&quot;);

foreach(car c in arrayOfCars) Console.WriteLine(c.Make + &quot;\t\t&quot; + c.Year);

// Demo descending sort of numeric value using IComparer. Array.Sort(arrayOfCars,car.sortYearDescending); Console.WriteLine(&quot;\nArray - Sorted by Year (Descending - IComparer)\n&quot;);

foreach(car c in arrayOfCars) Console.WriteLine(c.Make + &quot;\t\t&quot; + c.Year); Console.ReadLine; }  } }                     Add a class to the project. Name the class car.  Replace the code in Car.cs with the following: using System; using System.Collections; namespace ConsoleEnum {     public class car : IComparable {           // Beginning of nested classes.

// Nested class to do ascending sort on year property. private class sortYearAscendingHelper: IComparer {        int IComparer.Compare(object a, object b)         { car c1=(car)a; car c2=(car)b;

if (c1.year > c2.year) return 1;

if (c1.year < c2.year) return -1;

else return 0; }     }

// Nested class to do descending sort on year property. private class sortYearDescendingHelper: IComparer {        int IComparer.Compare(object a, object b)         { car c1=(car)a; car c2=(car)b;

if (c1.year < c2.year) return 1;

if (c1.year > c2.year) return -1;

else return 0; }     }

// Nested class to do descending sort on make property. private class sortMakeDescendingHelper: IComparer {        int IComparer.Compare(object a, object b)         { car c1=(car)a; car c2=(car)b; return String.Compare(c2.make,c1.make); }     }

// End of nested classes.

private int year; private string make; public car(string Make,int Year) {        make=Make; year=Year; }

public int Year {        get  {return year;} set {year=value;} }

public string Make {        get {return make;} set {make=value;} }

// Implement IComparable CompareTo to provide default sort order. int IComparable.CompareTo(object obj) {        car c=(car)obj; return String.Compare(this.make,c.make); }

// Method to return IComparer object for sort helper. public static IComparer sortYearAscending {              return (IComparer) new sortYearAscendingHelper; }

// Method to return IComparer object for sort helper. public static IComparer sortYearDescending {              return (IComparer) new sortYearDescendingHelper; }

// Method to return IComparer object for sort helper. public static IComparer sortMakeDescending {             return (IComparer) new sortMakeDescendingHelper; }

} }                     Run the project. The following output appears in the Console window: Array - Unsorted

Ford           1992 Fiat           1988 Buick          1932 Ford           1932 Dodge          1999 Honda          1977

Array - Sorted by Make (Ascending - IComparable)

Buick          1932 Dodge          1999 Fiat           1988 Ford           1932 Ford           1992 Honda          1977

Array - Sorted by Year (Ascending - IComparer)

Ford           1932 Buick          1932 Honda          1977 Fiat           1988 Ford           1992 Dodge          1999

Array - Sorted by Make (Descending - IComparer)

Honda          1977 Ford           1932 Ford           1992 Fiat           1988 Dodge          1999 Buick          1932

Array - Sorted by Year (Descending - IComparer)

Dodge          1999 Ford           1992 Fiat           1988 Honda          1977 Buick          1932 Ford           1932 

back to the top

Keywords: kbhowtomaster KB320727

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.