Can some one explain me how comparator works? I mean when someone use return a-b or for example took from here (http://buttercola.blogspot.com/2015/08/leetcode-skyline-problem.html) :
public class EdgeComparator implements Comparator<Edge> {
@Override
public int compare(Edge a, Edge b) {
if (a.x != b.x) {
return a.x - b.x;
}
if (a.isLeft && b.isLeft) {
return b.height - a.height;
}
if (!a.isLeft && !b.isLeft) {
return a.height - b.height;
}
return a.isLeft ? -1 : 1;
}
}
Say for example, here why they are using a.height - b.height ? or b.height - a.height ? Please explain me.