IComparable的和IComparer的区别 [英] difference between IComparable and IComparer
问题描述
可能重复:结果
的何时使用IComparable的< T>与的IComparer< T>
是IComparable接口和IComparer的接口之间有什么区别?是否有必要用的Array.Sort()方法。
what is the difference between IComparable and IComparer Interfaces? Is it necessary to use this interface always with Array.Sort() method
推荐答案
一直使用这个接口。顾名思义, IComparable的< T>
读出的我可比的。 IComparable的< T>
为 T
允许定义,当你的比较的的的当前实例的同类型的另一个实例。 的IComparer< T>
读出的我是一个比较器,我比较的。 的IComparer< T>
用于 T
中的任何两个实例比较,典型的外<$的实例范围C $ C> T 。
As the name suggests, IComparable<T>
reads out I'm comparable. IComparable<T>
when defined for T
lets you compare the current instance with another instance of same type. IComparer<T>
reads out I'm a comparer, I compare. IComparer<T>
is used to compare any two instances of T
, typically outside the scope of the instances of T
.
至于他们是什么可以在第一次混乱。从定义,它应该清楚,因此 IComparable的< T>
(在类中定义的 T
本身)应该是事实上的标准来进行排序提供的逻辑。默认排序
在列表< T>
等依赖于此。实施的IComparer< T>在
不利于正规排序。随后,对于实施 T
IComparable的<价值不大; T>
比 T
以外的任何其他类。这样的:
As to what they are for can be confusing at first. From the definition it should be clear that hence IComparable<T>
(defined in the class T
itself) should be the de facto standard to provide the logic for sorting. The default Sort
on List<T>
etc relies on this. Implementing IComparer<T>
on T
doesn't help regular sorting. Subsequently, there is little value for implementing IComparable<T>
on any other class other than T
. This:
class MyClass : IComparable<T>
很少是有道理的。
rarely makes sense.
在该另一方面
class T : IComparable<T>
{
public int CompareTo(T other)
{
//....
}
}
时应该怎么做。
的IComparer< T>
当你需要基于自定义顺序排序,而不是作为一个一般的规则是有用的。例如,在某些时候一类人
您可能需要排序根据他们的年龄的人。在这种情况下,你可以这样做:
IComparer<T>
can be useful when you require sorting based on a custom order, but not as a general rule. For instance, in a class of Person
at some point you might require to Sort people based on their age. In that case you can do:
class Person
{
public int Age;
}
class AgeComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Age - y.Age;
}
}
现在的 AgeComparer
有助于排序依据年龄
列表。
Now the AgeComparer
helps in sorting a list based on Age
.
var people = new Person[] { new Person { age = 23 }, new Person(){ age = 22 } };
people.Sort(p, new AgeComparer()); //person with age 22 comes first now.
同样的IComparer< C $ C> T
没有意义。
class Person : IComparer<Person>
true此工作,但并不好看眼睛和失败的逻辑。
True this works, but doesn't look good to eyes and defeats logic.
通常你需要的是 IComparable的< T>
。此外理想情况下你只能有一个 IComparable的< T>
而多个的IComparer< T>
可根据不同的标准。
Usually what you need is IComparable<T>
. Also ideally you can have only one IComparable<T>
while multiple IComparer<T>
is possible based on different criteria.
的IComparer< T>
和 IComparable的< T>
是完全类似于的IEqualityComparer< T>
和 IEquatable< T>
这是用于测试的平等而不是比较/排序;一个良好的线程 的,我写的完全一样的答案: )
The IComparer<T>
and IComparable<T>
are exactly analogous to IEqualityComparer<T>
and IEquatable<T>
which are used for testing equality rather than comparing/sorting; a good thread here where I wrote the exact same answer :)
这篇关于IComparable的和IComparer的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!