Sorting and Searching:IComparer as a Property : Compare « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » CompareScreenshots 
Sorting and Searching:IComparer as a Property
Sorting and Searching:IComparer as a Property

using System;
using System.Collections;

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

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;
}

           
       
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:Using IComparerSorting and Searching:Using IComparer
6.Sorting and Searching:Implementing IComparableSorting and Searching:Implementing IComparable
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.