How to sort an array or ArrayList<Point> ASC first by x and then by y?

Posted by newba on Stack Overflow See other posts from Stack Overflow or by newba
Published on 2010-04-30T01:46:50Z Indexed on 2010/04/30 1:57 UTC
Read the original article Hit count: 262

Filed under:
|
|
|
|

Hi everyone,

I just want to use Collections.sort or Arrays.sort to sort a list of points (class Point) by x first and then by y.

I have a class Ponto that implements Comparable like this:

public int compareTo(Ponto obj) {
        Ponto tmp = obj;
        if (this.x < tmp.x) {
            return -1;
        } else if (this.x > tmp.x) {
            return 1;
        }
        return 0;
    }

but now I want to sort by y too after x.

How can I do that by modifying the above code? Or is that a better and "clean" way to do this? I also use to pass this code to C++, in which I've created a structure called Point with a equivalent comparable method.

© Stack Overflow or respective owner

Related posts about java

Related posts about c++