IComparer as a Property : IComparer « Data Structure « C# / CSharp Tutorial

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » Data Structure » IComparer 
11. 42. 2. IComparer as a Property
using System;
using System.Collections;

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

class MainClass
{
    public static void Main()
    {
        Employee[] arr = new Employee[4];
        arr[0new Employee("A"1);
        arr[1new Employee("B"2);
        arr[2new Employee("C"4);
        arr[3new 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);
    }
}
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
11. 42. IComparer
11. 42. 1. Use IComparer
11. 42. 2. IComparer as a Property
11. 42. 3. Class with IComparable and IComparer
w_w__w___.__j__a__v__a___2__s.___c__om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.