wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reverse Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reverse Comparator" Watch "Reverse Comparator" New topic
Author

Reverse Comparator

Ai Ayumi
Greenhorn

Joined: May 26, 2012
Posts: 6
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:
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4377
    
    8

Well, think about what compareTo means. And realise that if a < b, then b > a.

Or to put it another way, if a comes before b in one ordering, then b will come before a in the reverse ordering.

There's actually a method in the Collections class that will reverse any Comparator.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: May 26, 2012
Posts: 6
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?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: May 26, 2012
Posts: 6

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reverse Comparator