如何使用IComparable和IComparer对对象列表进行排序 [英] How to sort a list of objects with IComparable and IComparer
问题描述
我正在尝试实现此链接的相同示例,但更多地针对依赖孩子的数量.
I'm trying to implement the same example of this link but more oriented on the number of dependent kids.
http://www.codeproject. com/Articles/42839/使用IComparable和IComparer-Inte排序列表
所以我有3名员工,分别为A:0,B:0,C:2.我想按孩子的数量顺序排列它们.所以我会得到C:2,B:0,A:0
So I have 3 employees with A: 0, B: 0, C: 2 respectively. I want to order them descendently by number of kids. So I would have C:2, B:0, A:0
但是我的清单未排序.它仍然是A:0,B:0,C:2
But my list it's not being sorted. It remains as A: 0, B: 0, C: 2
我做错了什么?
我的比较器
public class EmployeeComparer : IComparer<Employee>
{
public int Compare(Employee x, Employee y)
{
this.CompareNumberOfKids(x, y);
}
public int CompareNumberOfKids(Employee x, Employee y)
{
if (x.NumberOfKids > y.NumberOfKids)
{
return -1;
}
else if (x.NumberOfKids < y.NumberOfKids)
{
return 1;
}
else
{
return 0;
}
}
}
我的企业实体
public class Employee : IComparable<Employee>
{
//...//
Public NumberOfKids { get; set; }
int IComparable<Employee>.CompareTo(Employee next)
{
return new EmployeeComparer().Compare(this, next);
}
public override bool Equals(object obj)
{
if (obj != null && obj is Emmployee)
{
return ((Employee)obj).ID.Equals(this.ID);
}
else return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
Aspx.cs
public List<Employee> GetEmployeeSortedList()
{
List<Employee> list = new List<Employee>();
list.Add(new Employee() { Name = "A", NumberOfKids = 0 } );
list.Add(new Employee() { Name = "B", NumberOfKids = 0 } );
list.Add(new Employee() { Name = "C", NumberOfKids = 2 } );
list.Add(new Employee() { Name = "D", NumberOfKids = 1 } );
list.Add(new Employee() { Name = "E", NumberOfKids = 0 } );
list.Add(new Employee() { Name = "F", NumberOfKids = 4 } );
list = list.Take(3).ToList();
EmployeeComparer comp = new EmployeeComparer();
list.Sort(comp);
return list;
}
推荐答案
首先,如果您的Employee
类使用相同的排序条件实现IComparable<Employee>
,则无需按降序对IComparer<Employee>
进行排序.对于您的Employee
类来说,为每次比较实例化一个新的IComparer<Employee>
都是极其低效的.
First, there's no need to have an IComparer<Employee>
that sorts by descending if your Employee
class implements IComparable<Employee>
using the same sort criteria. And it's horribly inefficient for your Employee
class to instantiate a new IComparer<Employee>
for every comparision.
您应该更改您的Employee
类,以便其CompareTo
看起来像这样:
You should change your Employee
class so that its CompareTo
looks like this:
int CompareTo(Employee next)
{
return next.NumberOfKids.CompareTo(this.NumberOfKids);
}
然后,您可以完全放弃EmployeeComparer
并进行如下排序:
Then you can ditch the EmployeeComparer
altogether and sort like this:
list = list.Take(3).ToList();
list.Sort(); // Uses default IComparable for the Employee class
return list;
通常,您使类上的IComparable<T>
实现执行默认的排序顺序.对于员工而言,可能是按员工ID或姓氏,名字. IComparer<T>
实现应用于其他排序条件.
Typically, you make the IComparable<T>
implementation on the class perform the default sorting order. In the case of employees, that'd probably either be by employee ID or perhaps last name, first name. IComparer<T>
implementations should be for other sorting criteria.
但是,对于List<T>
,您还有另一个选择:使用匿名函数.例如,您可以这样编写:
With List<T>
, though, you have another option: use an anonymous function. For example, you could do this by writing:
list.Sort((x, y) => y.NumberOfKids.CompareTo(x.NumberOfKids));
请参见此列表.排序超载.
或者,您可以完全放弃IComparer<T>
和IComparable<T>
和List.Sort
的整个想法,并以LINQ方式进行操作:
Or, you could just ditch the whole idea of IComparer<T>
and IComparable<T>
and List.Sort
altogether and do it the LINQ way:
var result = list.Take(3).OrderByDescending(x => x.NumberOfKids).ToList();
这篇关于如何使用IComparable和IComparer对对象列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!