例9-15是這個例子的完整代碼。為了簡化例子,整型數組已經被移除,用於輸出的Employee的ToString()方法也增加了代碼以讓您看得到排序的效果。
例9-15 按員工的ID和工齡進行排序
using System;
using System.Collections.Generic;
using System.Text;
namespace IComparer
{
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;
}
}
//靜態方法,用於獲取一個Comparer對象
public static EmployeeComparer GetComparer()
{
return new Employee.EmployeeComparer();
}
public int CompareTo(Employee rhs)
{
return this.empID.CompareTo(rhs.empID);
}
//通過自定義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;
}
//實現IComparer接口的嵌套類
public class EmployeeComparer : IComparer<Employee>
{
private Employee.EmployeeComparer.ComparisonType
whichComparison;
//比較方式枚舉
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, WhichComparison);
}
public Employee.EmployeeComparer.ComparisonType
WhichComparison
{
get { return whichComparison; }
set { whichComparison = value; }
}
}
}
public class Tester
{
static void Main()
{
List<Employee> empArray = new List<Employee>();
Random r = new Random();
for (int i = 0; i < 5; i++)
{
//添加一個隨機的員工ID
empArray.Add(new Employee(
r.Next(10) + 100, r.Next(20)));
}
//顯示Employee數組的所有內容
for (int i = 0; i < empArray.Count; i++)
{
Console.Write("\n{0} ", empArray[i].ToString());
}
Console.WriteLine("\n");
//排序並顯示Employee數組
Employee.EmployeeComparer c = Employee.GetComparer();
c.WhichComparison =
Employee.EmployeeComparer.ComparisonType.EmpID;
empArray.Sort(c);
//顯示Employee數組的所有內容
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");
}
}
}
輸出結果:
ID: 103. Years of Svc: 11
ID: 108. Years of Svc: 15
ID: 107. Years of Svc: 14
ID: 108. Years of Svc: 5
ID: 102. Years of Svc: 0
ID: 102. Years of Svc: 0
ID: 103. Years of Svc: 11
ID: 107. Years of Svc: 14
ID: 108. Years of Svc: 15
ID: 108. Years of Svc: 5
ID: 102. Years of Svc: 0
ID: 108. Years of Svc: 5
ID: 103. Years of Svc: 11
ID: 107. Years of Svc: 14
ID: 108. Years of Svc: 15
第一塊輸出顯示的是Employee對象被加進List時的情形。員工ID值和工齡是隨機順序的。第二塊顯示的是按員工ID排序後的結果,第三塊顯示的是按工齡排序後的結果。
如果您如例9-11那樣創建自己的集合,並希望實現IComparer,可能需要確保所有放在列表中的類型都實現了IComparer接口(這樣他們才有可能被排序),這可以通過前面講述的約束來實現。