谁能解释为什么当我使用 Enumerable.OrderBy 时.net 框架不调用我的比较器的比较方法。而当我使用 List.Sort() 时它会被调用。
// 下面的代码取自 StackOverFlow.com 上的另一篇文章
class Employee
{
public string Name { get; set; }
public int Salary { get; set; }
}
class Employee_SortBySalaryByAscendingOrder : IComparer<Employee>
{
#region IComparer<Employee> Members
public int Compare(Employee x, Employee y)
{
if (x.Salary > y.Salary) return 1;
else if (x.Salary < y.Salary) return -1;
else return 0;
}
#endregion
}
private void TestSort(object sender, EventArgs e)
{
List<Employee> empList = new List<Employee>()
{
new Employee { Name = "a", Salary = 14000 },
new Employee { Name = "b", Salary = 13000 }
};
Employee_SortBySalaryByAscendingOrder eAsc =
new Employee_SortBySalaryByAscendingOrder();
// Sort Employees by salary by ascending order.
// Does not work
IOrderedEnumerable<Employee> orderedEmployees = empList.OrderBy(x => x, eAsc);
// Works
empList.Sort(eAsc);
}