0

Im trying to write a Comparator which compares two objects of the class Coordinate. The Coordinate class is Pretty simple:

public class Coordinate {

    private int x, y;

    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

}

Now I want the Comparator to compare the x and the y value for two instances of the class Coordinate. Here is an example:
I have a Coordinate c1 which has x = 42 and y = 23. My second Coordinate c2 has x = 23 and y = 54. Now i put them both in an ArrayList an want to sort the List. I wanted to be sorted like the following:
The Coordinate with the lowest y value goes Always first, when you have two Coordinates having the same y value the Coordinate goes first which has a lower x value.
Example:

c1 (y = 4, x = 5 ) < c2 (y = 4, x = 6) < c3 (y = 5, x = 2)  

So how can I write a Comparator for this Purpose?
Thank you very much!

2

2 Answers 2

3
Comparator<Coordinate> c = Comparator.comparingInt(Coordinate::getY)
                                     .thenComparingInt(Coordinate::getX);

You can build compound comparators by means of thenComparing and thenComparingX.

var list = List.of(
        new Coordinate(6, 4),
        new Coordinate(2, 5),
        new Coordinate(5, 4)
);

list.sort(c);
System.out.println(list);

The snippet prints

[{y=4, x=5}, {y=4, x=6}, {y=5, x=2}]
1
  • Using Comparator


import java.util.ArrayList;
import java.util.Comparator;

class Coordinate {
    private int x, y;

    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String toString() {
        return "x = " + x + " y = " + y;
    }
}

public class Temp {
    public static void main(String[] args) {
        ArrayList<Coordinate> A = new ArrayList<>();
        A.add(new Coordinate(1, 2));
        A.add(new Coordinate(2, 1));
        A.add(new Coordinate(3, 2));
        A.sort(new Comparator<Coordinate>() {

            @Override
            public int compare(Coordinate o1, Coordinate o2) {
                if (o1.getY() < o2.getY()) {
                    return -1;
                } else if (o1.getY() > o2.getY()) {
                    return 1;
                } else {
                    if (o1.getX() < o2.getX()) {
                        return -1;
                    } else if (o1.getX() > o2.getX()) {
                        return 1;
                    }
                    return 0;
                }
            }
        });
        System.out.println(A.toString());
    }
}
  • Using Comparable Interface


import java.util.ArrayList;

class Coordinate implements Comparable<Coordinate> { # Notice implementing Comparable interface
    private int x, y;

    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public int compareTo(Coordinate o) { # implementing the abstract method of Comparable interface
        if (y < o.y) {
            return -1; 
        } else if (y > o.y) {
            return 1;
        } else {
            if (x < o.x) {
                return -1;
            } else if (x > o.x) {
                return 1;
            }
            return 0;
        }
    }

    public String toString() {
        return "x = " + x + " y = " + y;
    }
}

public class Temp {
    public static void main(String[] args) {
        ArrayList<Coordinate> A = new ArrayList<>();
        A.add(new Coordinate(1, 2));
        A.add(new Coordinate(2, 1));
        A.add(new Coordinate(3, 2));
        A.sort(null);
        System.out.println(A.toString());
    }
}


Output

[x = 2 y = 1, x = 1 y = 2, x = 3 y = 2]

Not the answer you're looking for? Browse other questions tagged or ask your own question.