Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IComparer for sorting

I am trying to use an IComparer to sort a list of Points. Here is the IComparer class:

public class CoordinatesBasedComparer : IComparer {     public int Compare(Object q, Object r)     {         Point a = (p)q;         Point b = (p)r;         if ((a.x == b.x) && (a.y == b.y))             return 0;         if ((a.x < b.x) || ((a.x == b.x) && (a.y < b.y)))             return -1;          return 1;     } } 

In the client code, I am trying to using this class for sorting a list of points p (of type List<Point>):

CoordinatesBasedComparer c = new CoordinatesBasedComparer(); Points.Sort(c); 

The code errors out. Apparently it is expecting IComparer<Point> as argument to sort method.
What do I need to do to fix this?

like image 872
Aadith Ramia Avatar asked Jan 15 '13 11:01

Aadith Ramia


People also ask

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.

What is the difference between comparable and IComparer?

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 .

Why do we use IComparable in C#?

Use the IComparable Interface in C# to sort elements. It is also used to compare the current instance with another object of same type. It provides you with a method of comparing two objects of a particular type. Remember, while implementing the IComparable interface, CompareTo() method should also be implemented.

What is IComparable C#?

C# provides an IComparable interface. This interface provides different types of type-specific comparison methods which means a value type or a class can implement this interface to sort its instances because we cannot sort the instances of a class directly because the compiler does not know on which basis to sort.


2 Answers

You need to implement the strongly type interface (MSDN).

public class CoordinatesBasedComparer : IComparer<Point> {     public int Compare(Point a, Point b)     {         if ((a.x == b.x) && (a.y == b.y))             return 0;         if ((a.x < b.x) || ((a.x == b.x) && (a.y < b.y)))             return -1;          return 1;     } } 

BTW, I think you use too many braces, I believe they should be used only when they contribute to the compiler. This is my version:

if (a.x == b.x && a.y == b.y)     return 0; if (a.x < b.x || (a.x == b.x && a.y < b.y))     return -1; 

Just like I dislike people using return (0).


Note that if you target a .Net-3.5+ application you can use LINQ which is easier and even faster with sorting.

LINQ vesion can be something like:

var orderedList = Points.OrderBy(point => point.x)                         .ThenBy(point => point.y)                         .ToList(); 
like image 145
gdoron is supporting Monica Avatar answered Oct 15 '22 15:10

gdoron is supporting Monica


public class CoordinatesBasedComparer : IComparer, IComparer<Point> {     public int Compare(Point a, Point b)     {         if ((a.x == b.x) && (a.y == b.y))             return 0;         if ((a.x < b.x) || ((a.x == b.x) && (a.y < b.y)))             return -1;          return 1;     }     int IComparer.Compare(Object q, Object r)     {         return Compare((Point)q, (Point)r);                 } } 
like image 24
Marc Gravell Avatar answered Oct 15 '22 14:10

Marc Gravell