⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-09-14

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 09-14: Sorting an array by employees' IDs and years of service

namespace Programming_CSharp
{
   using System;
   using System.Collections;
  
   // a simple class to store in the array
   public class Employee : IComparable
   {
      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();
      }

      // static method to get a Comparer object
      public static EmployeeComparer GetComparer()
      {
         return new Employee.EmployeeComparer();
      }

      // Comparer delegates back to Employee
      // Employee uses the integer's default
      // CompareTo method
      public int CompareTo(Object rhs)
      {
         Employee r = (Employee) rhs;
         return this.empID.CompareTo(r.empID);
      }

      // Special implementation to be called by custom comparer
      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;

      }

      // nested class which implements IComparer
      public class EmployeeComparer : IComparer
      {
         // enumeration of comparsion types
         public enum ComparisonType
         {
            EmpID,
            Yrs
         };

         // Tell the Employee objects to compare themselves
         public int Compare(object lhs, object rhs)
         {
            Employee l = (Employee) lhs;
            Employee r = (Employee) rhs;
            return l.CompareTo(r,WhichComparison);
         }

         public Employee.EmployeeComparer.ComparisonType 
            WhichComparison
         {
            get
            {
               return whichComparison;
            }
            set
            {
               whichComparison=value;
            }
         }

         // private state variable
         private Employee.EmployeeComparer.ComparisonType 
            whichComparison;
      }
      private int empID;
      private int yearsOfSvc = 1;
   }
   public class Tester
   {
      static void Main()
      {
         ArrayList empArray = new ArrayList();

         // generate random numbers for 
         // both the integers and the
         // employee id's
         Random r = new Random();

         // populate the array
         for (int i = 0;i<5;i++)
         {
            // add a random employee id
            empArray.Add(
               new Employee(
                  r.Next(10)+100,r.Next(20)
               )
            );
         }
         
         // display all the contents of the Employee array
         for (int i = 0;i<empArray.Count;i++)
         {
            Console.Write("\n{0} ", empArray[i].ToString());
         }
         Console.WriteLine("\n");

         // sort and display the employee array
         Employee.EmployeeComparer c = Employee.GetComparer();
         c.WhichComparison=Employee.EmployeeComparer.ComparisonType.EmpID;
         empArray.Sort(c);
         // display all the contents of the Employee array
         for (int i = 0;i<empArray.Count;i++)
         {
            Console.Write("\n{0} ", empArray[i].ToString());
         }
         Console.WriteLine("\n");

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -