# Reverse Comparator

Ai Ayumi

Greenhorn

Posts: 6

Matthew Brown

Bartender

Posts: 4566

8

posted 3 years ago

for real depth you have to delve in algorithms and the way sort method is implemented. it uses some modified version of merge sort known as Tim Sort.

if a<b, compare returns -negative

if a>b it returns +positive

if a=b it returns 0

so keeping this in mind , visualize that a<b , so the compare implementation given above will do b.compareTo(a) . now the same thing holds for compareTo() method as well. it will return negative if b<a, positive if b>a and 0 if b=a. in this case it will return positive which means the compare() method return positive. which means (to sort method) that a>b, which means a is greater/bigger than b and should be placed latter in the list. so b comes before and a comes later and we get reverse sort. i faced the same problem and that is how i think . but you can get more details from the sort method and the algorithmic implementation of the tim sort.

Ai Ayumi wrote:Hi all,

I saw in the book that when you want a reversed Comparator, you can simply swap the arguments used to invoke compareTo(). Why is that?

For example:

for real depth you have to delve in algorithms and the way sort method is implemented. it uses some modified version of merge sort known as Tim Sort.

if a<b, compare returns -negative

if a>b it returns +positive

if a=b it returns 0

so keeping this in mind , visualize that a<b , so the compare implementation given above will do b.compareTo(a) . now the same thing holds for compareTo() method as well. it will return negative if b<a, positive if b>a and 0 if b=a. in this case it will return positive which means the compare() method return positive. which means (to sort method) that a>b, which means a is greater/bigger than b and should be placed latter in the list. so b comes before and a comes later and we get reverse sort. i faced the same problem and that is how i think . but you can get more details from the sort method and the algorithmic implementation of the tim sort.

Ai Ayumi

Greenhorn

Posts: 6

posted 3 years ago

Ok I see....So by default, sort() thinks you are comparing 1st argument to 2nd in compare()?

compareTo within gives result based on 2nd compared to 1st, which sort() takes as 1st compared to 2nd....so it's reversed. Something like that?

gurpeet singh wrote:

for real depth you have to delve in algorithms and the way sort method is implemented. it uses some modified version of merge sort known as Tim Sort.

if a<b, compare returns -negative

if a>b it returns +positive

if a=b it returns 0

so keeping this in mind , visualize that a<b , so the compare implementation given above will do b.compareTo(a) . now the same thing holds for compareTo() method as well. it will return negative if b<a, positive if b>a and 0 if b=a. in this case it will return positive which means the compare() method return positive. which means (to sort method) that a>b, which means a is greater/bigger than b and should be placed latter in the list. so b comes before and a comes later and we get reverse sort. i faced the same problem and that is how i think . but you can get more details from the sort method and the algorithmic implementation of the tim sort.

Ok I see....So by default, sort() thinks you are comparing 1st argument to 2nd in compare()?

compareTo within gives result based on 2nd compared to 1st, which sort() takes as 1st compared to 2nd....so it's reversed. Something like that?

posted 3 years ago

Put simply a.compareTo(b) means if the result is negative a <b , if positive a > b and if 0 a=b.

for b.compare(a) just interchange a and b in the mathematical inequalities.

what i suggest is think of the sort() method as high level method that goes by its name and sorts the thing you pass to it. it uses the comparison rule that you give it through natural ordering or by passing Comparator. if you are really curious how sort works then you have to dig deeper into the scary world of algorithim and design.

Ai Ayumi wrote:gurpeet singh wrote:

for real depth you have to delve in algorithms and the way sort method is implemented. it uses some modified version of merge sort known as Tim Sort.

if a<b, compare returns -negative

if a>b it returns +positive

if a=b it returns 0

so keeping this in mind , visualize that a<b , so the compare implementation given above will do b.compareTo(a) . now the same thing holds for compareTo() method as well. it will return negative if b<a, positive if b>a and 0 if b=a. in this case it will return positive which means the compare() method return positive. which means (to sort method) that a>b, which means a is greater/bigger than b and should be placed latter in the list. so b comes before and a comes later and we get reverse sort. i faced the same problem and that is how i think . but you can get more details from the sort method and the algorithmic implementation of the tim sort.

Ok I see....So by default, sort() thinks you are comparing 1st argument to 2nd in compare()?

compareTo within gives result based on 2nd compared to 1st, which sort() takes as 1st compared to 2nd....so it's reversed. Something like that?

Put simply a.compareTo(b) means if the result is negative a <b , if positive a > b and if 0 a=b.

for b.compare(a) just interchange a and b in the mathematical inequalities.

what i suggest is think of the sort() method as high level method that goes by its name and sorts the thing you pass to it. it uses the comparison rule that you give it through natural ordering or by passing Comparator. if you are really curious how sort works then you have to dig deeper into the scary world of algorithim and design.

Ai Ayumi

Greenhorn

Posts: 6

posted 3 years ago

I acutally did look at sort() in Java API, mergesort, etc....and faile to fully understand it...scary stuff indeed

Thanks a lot for your explanations!

Put simply a.compareTo(b) means if the result is negative a <b , if positive a > b and if 0 a=b.

for b.compare(a) just interchange a and b in the mathematical inequalities.

what i suggest is think of the sort() method as high level method that goes by its name and sorts the thing you pass to it. it uses the comparison rule that you give it through natural ordering or by passing Comparator. if you are really curious how sort works then you have to dig deeper into the scary world of algorithim and design.

I acutally did look at sort() in Java API, mergesort, etc....and faile to fully understand it...scary stuff indeed

Thanks a lot for your explanations!