I understand that when implement compare method of Comparator interface we need to return
- +1 if o1 > o2
- -1 if o1 < o2
- 0 if o1 == o2
my question is why we need to return 0 when both equal? what is the use case or where it being used? if we considered sorting when o2 greater than to o1 or o2 equal to o1 does not change it place. can anyone come explain practical use case for this?
Java documentation says
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Does this mean return -1 or return 0 has same impact?
zero, or a positive integer
@Override
public int compare(Test f1, Test f2) {
if (f1.getId() > f2.getId()) {
return 1;
} else if (f1.getId() < f2.getId()) {
return -1;
} else {
return 0;
}
}