How to use the IComparable and IComparer interfaces in Visual C# (320727)



The information in this article applies to:

  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2002)

This article was previously published under Q320727
For a Microsoft Visual Basic .NET version of this article, see 322025.

IN THIS TASK

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.
  1. In Visual C#, create a new Console Application project. Name the application ConsoleEnum.
  2. 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("Ford",1992),
                new car("Fiat",1988),
                new car("Buick",1932),
                new car("Ford",1932),
                new car("Dodge",1999),
                new car("Honda",1977)
             };
          
             // Write out a header for the output.
             Console.WriteLine("Array - Unsorted\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
          
             // Demo IComparable by sorting array with "default" sort order.
             Array.Sort(arrayOfCars);
             Console.WriteLine("\nArray - Sorted by Make (Ascending - IComparable)\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
    
             // Demo ascending sort of numeric value with IComparer.
             Array.Sort(arrayOfCars,car.sortYearAscending());
             Console.WriteLine("\nArray - Sorted by Year (Ascending - IComparer)\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
    
             // Demo descending sort of string value with IComparer.
             Array.Sort(arrayOfCars,car.sortMakeDescending());
             Console.WriteLine("\nArray - Sorted by Make (Descending - IComparer)\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
    
             // Demo descending sort of numeric value using IComparer.
             Array.Sort(arrayOfCars,car.sortYearDescending());
             Console.WriteLine("\nArray - Sorted by Year (Descending - IComparer)\n");
    
             foreach(car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);
         
             Console.ReadLine();
          }
       }
    }
    					
  3. Add a class to the project. Name the class car.
  4. 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();
          }
    
       }
    }
    					
  5. 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

Modification Type:MajorLast Reviewed:1/18/2006
Keywords:kbHOWTOmaster KB320727 kbAudDeveloper