Sorting and Searching:Using IComparer : Compare « Collections Data Structure « C# / C Sharp






Sorting and Searching:Using IComparer

Sorting and Searching:Using IComparer

using System;
using System.Collections;

public class SortingandSearchingUsingIComparer1
{
    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, (IComparer) new Employee.SortByNameClass());
        // employees is now sorted by name
        
        foreach (Employee emp in arr)
        Console.WriteLine("Employee: {0}", emp);
        
        Array.Sort(arr, (IComparer) new Employee.SortByIdClass());
        // 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((IComparer) new Employee.SortByNameClass());
        
        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);
    }
}

public class Employee: IComparable
{
    public Employee(string name, int id)
    {
        this.name = name;
        this.id = id;
    }
    
    int IComparable.CompareTo(object obj)
    {
        Employee emp2 = (Employee) obj;
        if (this.id > emp2.id)
        return(1);
        if (this.id < emp2.id)
        return(-1);
        else
        return(0);
    }
    
    public override string ToString()
    {
        return(name + ":" + id);
    }
    
    public class SortByNameClass: IComparer
    {
        public int Compare(object obj1, object obj2)
        {
            Employee emp1 = (Employee) obj1;
            Employee emp2 = (Employee) obj2;
            
            return(String.Compare(emp1.name, emp2.name));
        }
    }
    
    public class SortByIdClass: IComparer
    {
        public int Compare(object obj1, object obj2)
        {
            Employee emp1 = (Employee) obj1;
            Employee emp2 = (Employee) obj2;
            
            return(((IComparable) emp1).CompareTo(obj2));
        }
    }
    
    string    name;
    int    id;
}

           
       








Related examples in the same category

1.implements the IComparable interfaceimplements the IComparable interface
2.Use IComparerUse IComparer
3.Implement IComparableImplement IComparable
4.Sorting and Searching:Advanced Use of HashesSorting and Searching:Advanced Use of Hashes
5.Sorting and Searching:Implementing IComparableSorting and Searching:Implementing IComparable
6.Sorting and Searching:IComparer as a PropertySorting and Searching:IComparer as a Property