import java.util.Comparator; public class Coord3 implements Comparable { final int x, y; public Coord3(int x, int y) { this.x = x; this.y = y; } // Standard compare... final static Comparator COMP = new Comparator<>() { @Override public int compare(Coord3 a,Coord3 b) { int diff = 0; if(diff == 0) diff = a.x - b.x; if(diff == 0) diff = a.y - b.y; return diff; } }; // Reverse compare! final static Comparator RCOMP = new Comparator<>() { @Override public int compare(Coord3 a,Coord3 b) { int diff = 0; if(diff == 0) diff = b.x - a.x; if(diff == 0) diff = b.y - a.y; return diff; } }; @Override public int compareTo(Coord3 that) { return COMP.compare(this, that); } }