This question already has an answer here:
What is the difference between IComparable
and IComparer
Interfaces? Is it necessary to use this interface always with Array.Sort()
method
This question already has an answer here:
What is the difference between IComparable
and IComparer
Interfaces? Is it necessary to use this interface always with Array.Sort()
method
As the name suggests,
IComparable<T>
reads out I'm comparable.IComparable<T>
when defined forT
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 ofT
, typically outside the scope of the instances ofT
.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 classT
itself) should be the de facto standard to provide the logic for sorting. The defaultSort
onList<T>
etc relies on this. ImplementingIComparer<T>
onT
doesn't help regular sorting. Subsequently, there is little value for implementingIComparable<T>
on any other class other thanT
. This:rarely makes sense.
On the other hand
is how it should be done.
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 ofPerson
at some point you might require to Sort people based on their age. In that case you can do:Now the
AgeComparer
helps in sorting a list based onAge
.Similarly
IComparer<T>
onT
doesn't make sense.True this works, but doesn't look good to eyes and defeats logic.
Usually what you need is
IComparable<T>
. Also ideally you can have only oneIComparable<T>
while multipleIComparer<T>
is possible based on different criteria.The
IComparer<T>
andIComparable<T>
are exactly analogous toIEqualityComparer<T>
andIEquatable<T>
which are used for testing equality rather than comparing/sorting; a good thread here where I wrote the exact same answer :)Objects that are instances of classes that implement
IComparable
can be compared to other objects. (ReadIComparable
as "I can be compared.") The method on this interface that does this isCompareTo
. This is useful if you want instances of such classes to know how to compare themselves to other objects.Objects that are instances of classes that implement
IComparer
can be used to compare pairs of objects. (ReadIComparer
as "I can compare.") The method on this interface that does this isCompare
. This is useful if you want to decouple the comparison logic from the class of objects being compared. One case where you might use this if you have various ways of comparing objects (this case-insensitive versus case-sensitive string comparisons).IComparer compares two objects that it's given. IComparable is implemented by the object that is being compared, for the purpose of comparing with another one of itself.
It is a good idea to implement IComparable for sorting objects. IComparable can be useful for sorting by different criteria (for example, sorting by a selectable field in the object).
Also see: When to use IComparable<T> Vs. IComparer<T>
The best explanation I've read is "The object to be sorted will implement IComparable while the class that is going to sort the objects will implement IComparer." (source)
If the objects you're trying to sort do not implement IComparable, you'll need to create a class that implements IComparer (and accepts those object types for comparison) and pass it to the Array.Sort() method.
IComparable is used to provide a default sort order for your objects.
IComparer is to provide additional comparison mechanisms.
see this article for reference. http://support.microsoft.com/kb/320727