Csharp/CSharp Tutorial/Data Structure/IComparer
Class with IComparable and IComparer
<source lang="csharp">using System; using System.Collections; public class MainClass {
public static int Main(string[] args) { Employee[] empList = new Employee[5]; empList[0] = new Employee(123, "R"); empList[1] = new Employee(6, "M"); empList[2] = new Employee(6, "V"); empList[3] = new Employee(13, "N"); empList[4] = new Employee(6, "C"); if(empList[0] < empList[4]) Console.WriteLine("empList[0] < empList[4]"); else Console.WriteLine("empList[0] >= empList[4]"); return 0; }
} public class Employee : IComparable {
private class SortByEmployeemNameHelper : IComparer { public SortByEmployeemNameHelper(){} int IComparer.rupare(object o1, object o2) { Employee t1 = (Employee)o1; Employee t2 = (Employee)o2; return String.rupare(t1.EmployeemName, t2.EmployeemName); } } private int EmployeeID; private string petName; // properties. public int ID { get{return EmployeeID;} set{EmployeeID = value;} } public string EmployeemName { get{return petName;} set{petName = value;} } // Constructor set. public Employee(){} public Employee(int id, string name){ this.EmployeeID = id; this.petName = name; } // IComparable implementation. int IComparable.rupareTo(object o) { Employee temp = (Employee)o; if(this.EmployeeID > temp.EmployeeID) return 1; if(this.EmployeeID < temp.EmployeeID) return -1; else return 0; } // Property to return the SortByEmployeemName comparer. public static IComparer SortByEmployeemName{ get { return (IComparer)new SortByEmployeemNameHelper(); } } public static bool operator < (Employee c1, Employee c2) { IComparable itfComp = (IComparable)c1; return (itfComp.rupareTo(c2) < 0); } public static bool operator > (Employee c1, Employee c2) { IComparable itfComp = (IComparable)c1; return (itfComp.rupareTo(c2) > 0); } public static bool operator <= (Employee c1, Employee c2) { IComparable itfComp = (IComparable)c1; return (itfComp.rupareTo(c2) <= 0); } public static bool operator >= (Employee c1, Employee c2) { IComparable itfComp = (IComparable)c1; return (itfComp.rupareTo(c2) >= 0); }
}</source>
empList[0] >= empList[4]
IComparer as a Property
<source lang="csharp">using System; using System.Collections; public class Employee: IComparable {
public Employee(string name, int id) { this.name = name; this.id = id; } int IComparable.rupareTo(object obj) { Employee emp2 = (Employee) obj; if (this.id > emp2.id) return(1); if (this.id < emp2.id) return(-1); else return(0); } public static IComparer SortByName { get { return((IComparer) new SortByNameClass()); } } public static IComparer SortById { get { return((IComparer) new SortByIdClass()); } } public override string ToString() { return(name + ":" + id); } class SortByNameClass: IComparer { public int Compare(object obj1, object obj2) { Employee emp1 = (Employee) obj1; Employee emp2 = (Employee) obj2; return(String.rupare(emp1.name, emp2.name)); } } class SortByIdClass: IComparer { public int Compare(object obj1, object obj2) { Employee emp1 = (Employee) obj1; Employee emp2 = (Employee) obj2; return(((IComparable) emp1).rupareTo(obj2)); } } string name; int id;
} class MainClass {
public static void Main() { Employee[] arr = new Employee[4]; arr[0] = new Employee("A", 1); arr[1] = new Employee("B", 2); arr[2] = new Employee("C", 4); arr[3] = new Employee("D", 3); Array.Sort(arr, Employee.SortByName); Console.WriteLine("employees is now sorted by name"); foreach (Employee emp in arr) Console.WriteLine("Employee: {0}", emp); Array.Sort(arr, Employee.SortById); Console.WriteLine("employees is now sorted by id"); foreach (Employee emp in arr) Console.WriteLine("Employee: {0}", emp); ArrayList arrList = new ArrayList(); arrList.Add(arr[0]); arrList.Add(arr[1]); arrList.Add(arr[2]); arrList.Add(arr[3]); arrList.Sort(Employee.SortByName); foreach (Employee emp in arrList) Console.WriteLine("Employee: {0}", emp); arrList.Sort(); // default is by id foreach (Employee emp in arrList) Console.WriteLine("Employee: {0}", emp); }
}</source>
employees is now sorted by name Employee: A:1 Employee: B:2 Employee: C:4 Employee: D:3 employees is now sorted by id Employee: A:1 Employee: B:2 Employee: D:3 Employee: C:4 Employee: A:1 Employee: B:2 Employee: C:4 Employee: D:3 Employee: A:1 Employee: B:2 Employee: D:3 Employee: C:4
Use IComparer
<source lang="csharp">using System; 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.rupareTo(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); } }
}</source>
Product list before sorting: A Cost: $5.50 On hand: 3 B Cost: $8.90 On hand: 2 C Cost: $3.00 On hand: 4 D Cost: $1.80 On hand: 8 Product list after sorting: A Cost: $5.50 On hand: 3 B Cost: $8.90 On hand: 2 C Cost: $3.00 On hand: 4 D Cost: $1.80 On hand: 8