I want to know if it possible to write this method in one line. I need to compare y-coordinates and break ties using the x-coordinates.
// comparing y-coordinates and breaking ties by x-coordinates
public int compareTo(Point p) {
if (y < p.y) {
return -1;
}
if (y > p.y) {
return 1;
}
if (x < p.x) {
return -1;
}
if (x > p.x) {
return 1;
}
return 0;
}
“note. The question is asked out of interest to see if there is any original out of the box solution. It is evident that the presented
solution is good as”
This should work (but I wouldn’t recommend it for readability):
I haven’t bothered to check, but I would guess that the compiler generates exactly the same byte codes as for your original code.