0
class IntegerComparator implements Comparator<Integer>
{
@Override
public int compare(Integer o1, Integer o2) {
    if(o1 < o2)
        return 1;
    else if(o1 > o2)
        return -1;
    else
    return 0;
}   
}

It gives the descending order. I know it. I remember it blindly. I dont understand why the implementation has to be like this.

I expect the results to be ascending order. Because in ascending order, the o1 should be always less than o2 if you take any two adjacent elements. But It gives the descending order which I dont expect.

Can someone demystify the logic behind it

Edit:

  • When the elements are swapped. Is it when the compare returns 1 ?
3
  • 2
    Note that the Integer class has a static compare method, which does exactly what you want. You may also use the compareTo instance method. Commented Sep 26, 2016 at 10:09
  • I know both but what logic defines the ascending order and descending order. Commented Sep 26, 2016 at 10:16
  • Btw, if you just want to flip a comparator, since Java 8, Comparator::reversed will do that for you. Commented Sep 26, 2016 at 10:21

3 Answers 3

4

When te first argument is bigger than the other, result has to be a positive number. When the first argument is smaller - result is negative number. Your implementation gives descending order, because you are doing the opposite of what I just wrote. You can just multiply that by -1 if you want ascending order. It comes from the fact that if you substract second number from the first number and the first number is bigger - result is gonna be positive.

Sign up to request clarification or add additional context in comments.

Comments

1

Remember that:

  • returning -1 put the first object before the second (generally value less than 0),
  • returning 0 do nothing,
  • returning 1 put the first object after the second (generally value more than 0).

This is repeated over all the object to compare and you have a ascending sorting. See bubble sorting algo on google/youtube.

You can see:

Collections.reverseOrder(myComparator);

To get a new Comparator with a reverse order of yours.

enter image description here

In this animation:

  • a > b -> return +1 (a moving right, after b)
  • a < b -> return -1 (a moving left, before b)

3 Comments

So does 1 mean swap the elements and 0 means do nothing?
0 do nothing yes, other values swap objects.
Thanks Tokazio. U got my question.
1

Comparison is based on subtraction. At the CPU level this is what it does.

It is an optimised form of.

long l = (long) o1 - o2; // use a long to avoid overflow.
if (l < 0) return -1;
if (l > 0) return +1;
return 0;

It returns a negative number if o1 - o2 is negative i.e. o1 < o2

It return a positive number if o1 - o2 is positive i.e. o1 > o2

and it return 0 if o1 - o2 is zero i.e. o1 == o2

2 Comments

Yeah. But what defines the ascending order and descending order.
@omkarsirra the caller (the sort algorithm) defines ascending order as matching the documentation e.g. as I just described. This definition is used as it is consistent with subtraction which is what the CPU actually does and is familiar to anyone who has been to primary school making it a good standard to use. It is descending order if you swap o1 and o2 as this makes up => down and down => up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.