Using IComparer for sorting
Solution 1:
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();
Solution 2:
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);
}
}