In depth analysis of IComparable and IComparer sorting examples

  • 2020-05-17 06:09:47
  • OfStack

As follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace hgoApp
{
    class Comparer
    {
        static void Main()
        {
            Employee[] Employees = new Employee[5];

            Employees[0] = new Employee(" zhang 3", 2800);
            Employees[1] = new Employee(" li 4", 1800);
            Employees[2] = new Employee(" The king 5", 5800);
            Employees[3] = new Employee(" The horse 6", 12800);
            Employees[4] = new Employee(" money 7", 8800);
            Console.WriteLine(" Sort by name ");
            Array.Sort(Employees);
            foreach (Employee em in Employees)
            {
                Console.WriteLine(em);
            }
            Console.WriteLine(" Sort by salary ");
            Array.Sort(Employees, Employee.SalaryCom);
            foreach (Employee em in Employees)
            {
                Console.WriteLine(em);
            }
        }
    }
    class Employee : IComparable
    {
        private string _Name;
        public string Name
        {
            get { return _Name; }
        }
        private int _Salary;
        public int Salary
        {
            get { return _Salary; }
        }
        public Employee(string Name, int Salary)
        {
            _Name = Name;
            _Salary = Salary;
        }
        // Display interface implementation 
        int IComparable.CompareTo(object obj)
        {
            if (!(obj is Employee))
            {
                throw new ArgumentException(" not Employee class "); 
            }
            return _Name.CompareTo(((Employee)obj)._Name);
        }
        // provide 1 Common strongly typed overloaded versions 
        public int CompareTo(Employee Em)
        {
            return _Name.CompareTo(Em._Name);
        }
        // When the first 1 Time to instantiate Employee when ,_SalaryCom Is empty , When using SalaryCom when , Will be created 1 a SalaryCompare object , The first 2 Time, 3 When the time , You can use it directly _SalaryCom the 
        private static SalaryCompare _SalaryCom = null;
        public static IComparer SalaryCom
        {
            get
            {
                if (_SalaryCom == null)
                {
                    _SalaryCom = new SalaryCompare(); 
                }
                return _SalaryCom;
            }
        }
        // Nested classes ( This class is the one that sorts salaries )
        private class SalaryCompare:IComparer
        {
            // Using the specified IComparer right Array Sort the elements in 
            int IComparer.Compare(object obj1,object obj2)
            {
                if (!(obj1 is Employee) || !(obj2 is Employee))
                {
                    throw new ArgumentException(" not Employee class "); 
                }
                return ((Employee)obj1)._Salary.CompareTo(((Employee)obj2)._Salary);
            }
        }
        public override string ToString()
        {
            return _Name +"  "+ _Salary.ToString();
        }
    }
}

Related articles: