Comparator based on Double values in Java
Your question does not show how you call/use the Comparators you have defined. Despite that, there are a number of issues I see with your code, and feel I should point out....

When you have data that is sorted, the data tends to last around for a while, and sorted-data is often a very important assumption for complex processing (i.e. if the data was not sorted correctly the computations will fail).

Whenever you have sorted data, and Comparable data, the data should almost always be immutable (cannot possibly change). If the data can change after the sort happens, then the sorted data is no longer sorted.

So, your Point class should probably be an immutable class. At the moment, it has a number of problems with that:


The member fields should be final.
There should be 'getters' for the fields, and the fields themselves should be private (not public like you have).


Your class should look more like:

public static final class Point{
    private final int id;
    private final double x;
    private final double y;

    Point(int id, double x, double y){
        this.id = id;
        this.x = x;
        this.y = y;
    }

    public int getID() {
        return id;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    .....


ahhh, I see skiwi just answered a bunch... I'll cover some other stuff from here on....

Reiterating his comments:


the equals and hashCode method he suggests are very important (I was about to add that too).


Right, when you have value-based items like this, it is often convenient to make the Point have a 'natural' ordering. This means that the Point class should implement Comparable&lt;Point&gt;, and then implement the compareTo(Point other) method.

Having two comparators is a problem, you only want one, that does both coordinates.

Consider a compareTo() method like:

public static class Point implements Comparable&lt;Point&gt; {

    ....

    public int compareTo(final Point other) {

        if (other == null) {
            // non null always comes after null
            return 1;
        }

        final int xcomp = Double.compare(this.x, other.x);

        return xcomp == 0 ? Double.compare(this.y, other.y) : xcomp;
    }


This will be one comparator that does both axes.

Then, you can use this Point as a natural sort order, and use it as a key in a TreeMap, and or things like Arrays.sort(pointarray), etc.

Note, I reused the comparison function Double.compare() to do the heavy lifting of the compare.