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);
    }
}
5
  • " an error" . I wonder if you could guess my next quesstion.... Commented Feb 12, 2020 at 20:19
  • So sorry: "Failed to compare two elements in the array." Commented Feb 12, 2020 at 20:20
  • 1
    Don't use ArrayList instead use List<Point>. 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. Commented Feb 12, 2020 at 20:22
  • Same error here :( Commented Feb 12, 2020 at 20:25
  • Here's how you can setup a class to be comparable learn.microsoft.com/en-us/dotnet/api/… Commented Feb 12, 2020 at 20:26

1 Answer 1

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.