Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between IComparable and IComparer [duplicate]

What is the difference between IComparable and IComparer Interfaces? Is it necessary to use this interface always with Array.Sort() method

like image 798
Raghav55 Avatar asked May 12 '11 15:05

Raghav55


People also ask

What is IComparable?

The IComparable interface defines a generalized type-specific comparison method that a value type or class implements to order or sort its instances. The IComparable is implemented by types whose values can be ordered or sorted. The interface requires the CompareTo method to be implemented.

How does IComparer work in C#?

The IComparer. Compare method requires a tertiary comparison. 1, 0, or -1 is returned depending on whether one value is greater than, equal to, or less than the other. The sort order (ascending or descending) can be changed by switching the logical operators in this method.

Which of the following method is to be implemented for implementing IComparable interface?

The instance's IComparable implementation is called automatically by methods such as Array. Sort and ArrayList. Sort. The implementation of the CompareTo(Object) method must return an Int32 that has one of three values, as shown in the following table.


2 Answers

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.

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.

On the other hand

class T : IComparable<T> {     public int CompareTo(T other)     {         //....     } } 

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 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;     } } 

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. 

Similarly IComparer<T> on T doesn't make sense.

class Person : IComparer<Person> 

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 one IComparable<T> while multiple IComparer<T> is possible based on different criteria.

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 :)

like image 112
nawfal Avatar answered Oct 13 '22 14:10

nawfal


  • IComparable - Defines an interface for an object with a CompareTo() method that takes another object of the same type and compares the current object to the passed one. It internalizes the comparison to the object, allowing for a more inline comparison operation, and is useful when there's only one logical way, or an overwhelmingly common default way, to compare objects of a type.
  • IComparer - Defines an interface with a Compare() method that takes two objects of another type (which don't have to implement IComparable) and compares them. This externalizes the comparison, and is useful when there are many feasible ways to compare two objects of a type, or when the type doesn't implement IComparable (or the IComparable implementation compares a different way than what you want) and you don't have control over that type's source.
like image 25
KeithS Avatar answered Oct 13 '22 12:10

KeithS