Define your own comparasion type and Comparer : IComparable « Data Structure « C# / CSharp Tutorial

Home
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.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Data Structure » IComparable 
11.41.4.Define your own comparasion type and Comparer
using System;
using System.Collections.Generic;

    public class Employee : IComparable<Employee>
    {
        private int empID;

        private int yearsOfSvc = 1;

        public Employee(int empID)
        {
            this.empID = empID;
        }

        public Employee(int empID, int yearsOfSvc)
        {
            this.empID = empID;
            this.yearsOfSvc = yearsOfSvc;
        }

        public override string ToString()
        {
            return "ID: " + empID.ToString() ". Years of Svc: " + yearsOfSvc.ToString();
        }

        public bool Equals(Employee other
        {
            if (this.empID == other.empID)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static EmployeeComparer GetComparer()
        {
            return new Employee.EmployeeComparer();
        }

        public int CompareTo(Employee rhs)
        {
            return this.empID.CompareTo(rhs.empID);
        }

        public int CompareTo(Employee rhs,Employee.EmployeeComparer.ComparisonType which)
        {
            switch (which)
            {
                case Employee.EmployeeComparer.ComparisonType.EmpID:
                    return this.empID.CompareTo(rhs.empID);
                case Employee.EmployeeComparer.ComparisonType.Yrs:
                    return this.yearsOfSvc.CompareTo(rhs.yearsOfSvc);
            }
            return 0;

        }
        public class EmployeeComparer : IComparer<Employee>
        {
            public enum ComparisonType
            {
                EmpID,
                Yrs
            };

            public bool Equals(Employee lhs, Employee rhs)
            {
                return this.Compare(lhs, rhs== 0;
            }

            public int GetHashCode(Employee e)
            {
                return e.GetHashCode();
            }
            public int Compare(Employee lhs, Employee rhs)
            {
                return lhs.CompareTo(rhs, EmpCompareType);
            }

            public Employee.EmployeeComparer.ComparisonType
                EmpCompareType {get; set;}
        }
    }
    public class Tester
    {
        static void Main()
        {
            List<Employee> empArray  = new List<Employee>();
            Random r = new Random();
            for (int i = 0; i < 5; i++)
            {
                empArray.Add(new Employee(r.Next(10100, r.Next(20)));
            }
            Employee.EmployeeComparer c = Employee.GetComparer();
            c.EmpCompareType = Employee.EmployeeComparer.ComparisonType.EmpID;
            empArray.Sort(c);
            for (int i = 0; i < empArray.Count; i++)
            {
                Console.WriteLine(empArray[i].ToString());
            }
            c.EmpCompareType = Employee.EmployeeComparer.ComparisonType.Yrs;
            empArray.Sort(c);

            for (int i = 0; i < empArray.Count; i++)
            {
                Console.WriteLine(empArray[i].ToString());
            }
        }
    }
11.41.IComparable
11.41.1.Implement IComparable
11.41.2.Generic method for IComparable
11.41.3.ComplexNumber: Implement System.IComparable
11.41.4.Define your own comparasion type and Comparer
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.