1

When comparing objects it's common that you will end up with an integer other than -1, 0, 1.

e.g. (in Java)

Byte a = 10;
Byte b = 20;
System.out.println(a.compareTo(b)); // -10

Is there any algorithm, data-structure used in practice that takes advantage of this attribute of the comparison model?

Or in other words: why is any number > 1 or < -1 is a helpful piece of info?

Edit: I'm sorry. I see how you could've misinterpreted the question as a Java problem. My mistake. I changed the tag from "java" to "language agnostic".

4
  • Could you please elaborate? I don't understand what you're asking. Are you asking what the compareTo() method is used for? If so: to compare objects with each other, in particular when sorting them. Commented Nov 20, 2016 at 18:27
  • 1
    He is basically asking why to initially design compareTo as a method that returns int instead of a trinary enum/flag/int values/boolean. Or in other words: why is any number > 1 or < -1 is a helpful piece of info? Commented Nov 20, 2016 at 18:30
  • 2
    Possible duplicate of sorting algorithm where pairwise-comparison can return more information than -1, 0, +1 Commented Nov 20, 2016 at 18:34
  • Well, you just found one. Look at the implementation of Byte.compareTo(). It would be more complex if it had to return -1 or 1. Commented Nov 20, 2016 at 18:40

2 Answers 2

0

The contract of a Comparable object specifies that the value returned by compareTo() is:

A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The above definition simplifies comparisons, we just need to test the returned value against zero using the usual comparison operators. For instance, to check if object a is greater than or equal to object b we can write:

a.compareTo(b) >= 0

Also, this is more flexible than simply returning -1, 1 or 0 as it allows each implementation to return a value with additional information. For example, String's compareTo() returns:

The difference of the two character values at position k in the two strings -- that is, the value:

this.charAt(k) - anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

this.length() - anotherString.length()
Sign up to request clarification or add additional context in comments.

2 Comments

But what can I do with this additional information?
It depends on the problem... for the String example, you can tell how much is the difference
0

No algorithm will take advantage of this "attribute", because you cannot rely on the exact value returned.

The only guarantee you have, is that it will be <0, =0, or >0, because that is the contract defined by Comparable.compareTo():

Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The Byte implementation isn't any more specific:

Returns the value 0 if this Byte is equal to the argument Byte; a value less than 0 if this Byte is numerically less than the argument Byte; and a value greater than 0 if this Byte is numerically greater than the argument Byte (signed comparison).

Anything else is arbitrary and may change without notice.


To clarify, the returned value is defined to be <0, =0, or >0 instead of -1, 0, or +1 as a convenience to the implementation, not as a means to provide additional information to the caller.

As an example, the Byte.compareTo(Byte anotherByte) is implemented to return a number between -255 and 255 (inclusive) with this simple code:

return this.value - anotherByte.value;

The alternative would be code like:

return this.value < anotherByte.value ? -1 : this.value > anotherByte.value ? 1 : 0;

Since it's as easy for the caller to test the return value x < 0 instead of x == -1, allowing the broader range of return values provides for cleaner, more optimal code.

Comments

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.