Sorting and Searching:Advanced Use of Hashes : Compare « Collections Data Structure « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
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
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# / C Sharp » Collections Data Structure » CompareScreenshots 
Sorting and Searching:Advanced Use of Hashes
Sorting and Searching:Advanced Use of Hashes


using System;
using System.Collections;

public class AdvancedUseofHashes
{
    public static void Main()
    {
        Employee herb = new Employee("H"5);
        Employee george = new Employee("G"1);
        Employee frank = new Employee("F"2);
        Hashtable employees = 
        new Hashtable(Employee.HashByName, Employee.SortByName);
        employees.Add(herb, "AA");
        employees.Add(george, "BB");
        employees.Add(frank, "CC");
        Employee herbClone = new Employee("H"000);
        string address =(stringemployees[herbClone];
        Console.WriteLine("{0} lives at {1}", herbClone, address);
    }
}

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 override int GetHashCode()
    {
        return(id);
    }
    public static IComparer SortByName
    {
        get
        {
            return((IComparernew SortByNameClass());
        }
    }
    
    public static IComparer SortById
    {
        get
        {
            return((IComparernew SortByIdClass());
        }
    }
    public static IHashCodeProvider HashByName
    {
        get
        {
            return((IHashCodeProvidernew HashByNameClass());
        }
    }
    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));
        }
    }
    class HashByNameClass: IHashCodeProvider
    {
        public int GetHashCode(object obj)
        {
            Employee emp = (Employeeobj;
            return(emp.name.GetHashCode());
        }
    }
    
    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:Using IComparerSorting and Searching:Using IComparer
5. Sorting and Searching:Implementing IComparableSorting and Searching:Implementing IComparable
6. Sorting and Searching:IComparer as a PropertySorting and Searching:IComparer as a Property
w_ww.j___a__v_a2s.__c___o___m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.