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?
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.
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 .
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.
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.
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();
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With