0

How do I sort a List of Points by using an customized compare method?

using System;
using System.Collections;
using System.Collections.Generic;

public class Point : IComparer<Point>
{
    public int x;
    public int y;

    public Point(int x_Point, int y_Point)
    {
        x = x_Point;
        y = y_Point;
    }
    public int Compare(Point a, Point b)
    {
        if (a.x == b.x && a.y == b.y)
            return 0;
        if (a.y < b.y)
            return -1;
        if (a.y == b.y && a.x < b.x)
            return -1;
        return 1;
    }
}

The code below throws an error at AL.sort().

"Failed to compare two elements in the array." "ArgumentException: At least one object must implement IComparable"

I have no clue why. Did I described my own comparing method at the Points class wrong?

public class ArrayListTest
{
    public static void Main(string[] args)
    {
        ArrayList AL = new ArrayList();
        Random R = new Random();
        for (int i = 0; i < 10; i++)
        {
            Point p = new Point(R.Next(50), R.Next(50));
            AL.Add(p);
        }
        PrintValues(AL);
        AL.Sort();
        PrintValues(AL);
    }
}
aejsi5
  • 147
  • 8
  • _" an error"_ . I wonder if you could guess my next quesstion.... – Jeroen van Langen Feb 12 '20 at 20:19
  • So sorry: "Failed to compare two elements in the array." – aejsi5 Feb 12 '20 at 20:20
  • 1
    Don't use `ArrayList` instead use `List`. `ArrayList` is a left over from the dark days before generics. Also you should be implementing `IComparable` instead of `IComparer` as the former is to indicate that the type can compared to other instances of the same type and the later just indicates that it can compare two instances of a given type. – juharr Feb 12 '20 at 20:22
  • Same error here :( – aejsi5 Feb 12 '20 at 20:25
  • Here's how you can setup a class to be comparable https://learn.microsoft.com/en-us/dotnet/api/system.icomparable-1?view=netframework-4.8 – juharr Feb 12 '20 at 20:26

1 Answers1

2

You'd better use the IComparable<> interface.

"The object to be sorted will implement IComparable while the class that is going to sort the objects will implement IComparer."

Source: difference between IComparable and IComparer

public class Point : IComparable<Point>
{
    public int x;
    public int y;

    public Point(int x_Point, int y_Point)
    {
        x = x_Point;
        y = y_Point;
    }

    public int CompareTo(Point other)
    {
        if (this.x == other.x && this.y == other.y)
            return 0;
        if (this.y < other.y)
            return -1;
        if (this.y == other.y && this.x < other.x)
            return -1;
        return 1;
    }
}

public static void Main()
{
    var AL = new List<Point>(); // ditch the ArrayList for good... ;-)
    Random R = new Random();
    for (int i = 0; i < 10; i++)
    {
        Point p = new Point(R.Next(50), R.Next(50));
        AL.Add(p);
    }
    PrintValues(AL);
    AL.Sort();
    PrintValues(AL);

}
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57