且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何排序数组或ArrayList< Point> ASC先用x然后再用y?

更新时间:2023-10-02 22:56:34

通过 this.y 的相同比较替换返回0 obj.y

Replace return 0 by the same comparison algo on this.y and obj.y.

顺便说一下,在这里不需要重新分配 tmp 优化的图片可以是:

By the way, reassigning to tmp is unnecessary here. The optimized picture can look like:

public int compareTo(Ponto other) {
    if (this.x == other.x) {
        return (this.y < other.y) ? -1 : ((this.y == other.y) ? 0 : 1);
    } else {
        return (this.x < other.x) ? -1 : 1;
    }
}