# The logic behind compareTo()

Page 1 of 1

## 5 Replies - 1020 Views - Last Post: 10 March 2013 - 08:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314944&amp;s=3d09cdc379ef4bcdfbcbec3a724a8171&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

# The logic behind compareTo()

Posted 10 March 2013 - 07:49 AM

I'm having a little trouble understanding compareTo().

I know what it does and how to use it, what I'm confused by is the logic behind it?

public int compareTo(String a, String B)/>/> {
return b.compareTo(a);
}

What is the mechanism behind this line and how it reverses a list?

Any information will be greatly appreciated.
Is This A Good Question/Topic? 0

## Replies To: The logic behind compareTo()

### #2 sepp2k

• D.I.C Lover

Reputation: 2270
• Posts: 3,483
• Joined: 21-June 11

## Re: The logic behind compareTo()

Posted 10 March 2013 - 08:02 AM

That line calls the method compareTo on b with the argument a. The result will be less than 0 if b is smaller than a, greater than 0 if b is greater than a and equal to 0 if b is equal to a. The line does not reverse a list.

If the method were called compare instead of compareTo and if it were inside a class that implemented the Comparator interface, then that comparator could be used to sort a list of strings in descending order. The logic behind that is that the compare method would always return the opposite of what the "natural" comparator for strings would return (since it compares b to a instead of a to b).

This post has been edited by sepp2k: 10 March 2013 - 08:02 AM

### #3 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: The logic behind compareTo()

Posted 10 March 2013 - 08:19 AM

Thank you sepp2k, you filled in some missing links but I'm still suffering a logical gap - how does Java handle the returned int to influence the order of a list of strings? I just can't seem to relate numbers to strings in this case..

I compare "b" to "a" and I should receive a negative result, this will reverse natural order, but how does -1 turn {"a", "b", "c"} into {"c", "b", "a"}?

### #4 sepp2k

• D.I.C Lover

Reputation: 2270
• Posts: 3,483
• Joined: 21-June 11

## Re: The logic behind compareTo()

Posted 10 March 2013 - 08:30 AM

-1 doesn't turn anything into anything. As I said, compareTo doesn't reverse anything, it doesn't change the position of anything, it doesn't modify any list in any way. All that compareTo does is to compare two elements, returning a number less than, equal to or greater than 0 depending on whether the first object is less than, equal to or greater than the second object. -1 is just a way for compareTo to say "the first object is less than the second object".

A sort method can then simply call compareTo to find out which of the two elements that it's currently looking at is greater. And if you want to sort by something other than the order implemented by the objects' compareTo method, you pass in a comparator, so that the sort method can call that comparator's compare method rather then the objects' compareTo. So if you pass in a Comparator that calls b.compareTo(a), it will return a positive number if a.compareTo(b) would've returned a negative one and vice-versa. So this will cause the order of the sorted collection to be reversed.

This post has been edited by sepp2k: 10 March 2013 - 09:05 AM

### #5 baavgai

• Dreaming Coder

Reputation: 6298
• Posts: 13,461
• Joined: 16-October 07

## Re: The logic behind compareTo()

Posted 10 March 2013 - 08:39 AM

There are three fundamental states of comparison: ==, <. or >. A compareTo returns an int with the logic of 0 begin equalTo, less than 0 being less than (<) and greater than 0 begin greater that (>).

While this often expresses itself as -1,0, and 1, this is NOT required. The result could be -42 and it would indicate less than.

The compareTo for an integer is often written as return this.n - other.n;

To reverse order on a sort, you could reverse the sign on the compareTo result. If you were extending a class that had compareTo, you could simply write:
public int compareTo(Object other) { return -1 * super.compareTo(other); }

### #6 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: The logic behind compareTo()

Posted 10 March 2013 - 08:57 AM

This book never showed the original implementation of compareTo(). So I searched for one and now, with your information, it seems to make sense.

Thanks for the help.