IComparer interface

In this chapter you will learn:

  1. Different IComparer for different ways to compare

Use IComparer

The following code implements IComparer and creates a ProductComparer to compare products by name. We can create another class and implement IComparer to compare products by cost.

using System; /*from   j av  a2  s. c  om*/
using System.Collections; 
 
// Create an IComparer for Product objects. 
class ProductComparer : IComparer { 
  // Implement the IComparable interface. 
  public int Compare(object obj1, object obj2) { 
    Product a, b; 
    a = (Product) obj1; 
    b = (Product) obj2; 
    return a.name.CompareTo(b.name); 
  } 
} 
 
class Product { 
  public string name; 
  double cost; 
  int onhand; 
 
  public Product(string n, double c, int h) { 
    name = n; 
    cost = c; 
    onhand = h; 
  } 
 
  public override string ToString() { 
    return 
      String.Format("{0,-10}Cost: {1,6:C}  On hand: {2}", 
                    name, cost, onhand); 
  } 
} 
 
class IComparerDemo { 
  public static void Main() { 
    ProductComparer comp = new ProductComparer(); 
    ArrayList inv = new ArrayList(); 
     
    // Add elements to the list 
    inv.Add(new Product("A", 5.5, 3)); 
    inv.Add(new Product("B", 8.9, 2));    
    inv.Add(new Product("C", 3.0, 4)); 
    inv.Add(new Product("D", 1.8, 8)); 
 
    Console.WriteLine("Product list before sorting:"); 
    foreach(Product i in inv) { 
      Console.WriteLine("   " + i); 
    } 
    Console.WriteLine(); 
 
    // Sort the list using an IComparer. 
    inv.Sort(comp); 
 
    Console.WriteLine("Product list after sorting:"); 
    foreach(Product i in inv) { 
      Console.WriteLine("   " + i); 
    } 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to implement IConvertible interface
Home » C# Tutorial » System Interface
ICloneable interface
IComparable interface
IComparer interface
IConvertible interface
IDisposable interface
IEnumerable interface
IEquatable interface
IFormatProvider interface
IFormattable interface