C#_IComparerインスタンス-IDまたはyearOfscvソートの実装

4455 ワード

LISTのSortを呼び出すとIComparerのデフォルトインプリメンテーションが呼び出され、quicksortは各要素のCompareToのIComparableインプリメンテーションが呼び出されます
 
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace ComparerTest

{

    class Employee : IComparable<Employee>

    {

        private int empID;

        private int yearOfSvc = 1;



        public Employee(int empID)

        {

            this.empID = empID;

        }



        public Employee(int empID, int yearOfSvc)

        {

            this.empID = empID;

            this.yearOfSvc = yearOfSvc;

        }



        public override string ToString()

        {

            return "ID: " + empID.ToString()

                + "  yearOfSvc: " + yearOfSvc.ToString();

        }



        public bool Equals(Employee other)

        {

            if (this.empID == other.empID)

            {

                return true;

            }

            else

            {

                return false;

            }

        }



        //  Comparer       

        public static EmployeeComparer GetComparer()

        {

            return new Employee.EmployeeComparer();

        }

        //     Employee

        //Employee       

        //CompareTo  

        public int CompareTo(Employee rhs)

        {

            return this.empID.CompareTo(rhs.empID);

        }





        //   Comparer        

        public int CompareTo(Employee rhs,

            Employee.EmployeeComparer.ComparisonType whichComparision)

        {

            switch (whichComparision)

            { 

                case Employee.EmployeeComparer.ComparisonType.EmpID:

                    return this.empID.CompareTo(rhs.empID);

                case Employee.EmployeeComparer.ComparisonType.Yrs:

                    return this.yearOfSvc.CompareTo(rhs.yearOfSvc);

            }

            return 0;

        }



        //  IComparer    

        public class EmployeeComparer : IComparer<Employee>

        {

            private Employee.EmployeeComparer.ComparisonType whichComparision;



            public enum ComparisonType

            {

                EmpID,

                Yrs

            };



            public bool Equals(Employee lhs, Employee rhs)

            {

                return this.Compare(lhs, rhs) == 0;

            }

            // Empolyee      

            public int Compare(Employee lhs, Employee rhs)

            {

                return lhs.CompareTo(rhs, WhichComparison);

            }



            public int GetHashCode(Employee e)

            {

                return e.GetHashCode();

            }



            public Employee.EmployeeComparer.ComparisonType WhichComparison

            {

                set

                {

                    whichComparision = value;

                }

                get 

                {

                    return whichComparision;

                }



            }

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            List<Employee> empArray = new List<Employee>();



            Random ra = new Random();



            for (int i = 0; i < 5; i++)

            {

                empArray.Add(new Employee(ra.Next(10)+100,ra.Next(20)));

            }



            for (int i = 0; i < empArray.Count; i++)

            {

                Console.Write("
{0}",empArray[i].ToString()); } Console.WriteLine(); Console.WriteLine(); Employee.EmployeeComparer c = Employee.GetComparer(); c.WhichComparison = Employee.EmployeeComparer.ComparisonType.EmpID; empArray.Sort(c); for (int i = 0; i < empArray.Count; i++) { Console.Write("
{0}", empArray[i].ToString()); } Console.WriteLine(); Console.WriteLine(); Employee.EmployeeComparer c2 = Employee.GetComparer(); c.WhichComparison = Employee.EmployeeComparer.ComparisonType.Yrs; empArray.Sort(c2); for (int i = 0; i < empArray.Count; i++) { Console.Write("
{0}", empArray[i].ToString()); } Console.ReadLine(); } } }