What do the return values of Comparable.compareTo mean in Java?

JavaComparable

Java Problem Overview


What is the difference between returning 0, returning 1 and returning -1 in compareTo() in Java?

Java Solutions


Solution 1 - Java

Official Definition

From the reference docs of Comparable.compareTo(T):

> Compares this object with the > specified object for order. Returns a > negative integer, zero, or a positive > integer as this object is less than, > equal to, or greater than the > specified object. > > The implementor must ensure > sgn(x.compareTo(y)) == > -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must > throw an exception iff y.compareTo(x) > throws an exception.) > > The implementor must also ensure that > the relation is transitive: > (x.compareTo(y)>0 && y.compareTo(z)>0) > implies x.compareTo(z)>0. > > Finally, the implementor must ensure > that x.compareTo(y)==0 implies that > sgn(x.compareTo(z)) == > sgn(y.compareTo(z)), for all z. > > It is strongly recommended, but not > strictly required that > (x.compareTo(y)==0) == (x.equals(y)). > Generally speaking, any class that > implements the Comparable interface > and violates this condition should > clearly indicate this fact. The > recommended language is "Note: this > class has a natural ordering that is > inconsistent with equals." > > In the foregoing description, the > notation sgn(expression) designates > the mathematical signum function, > which is defined to return one of -1, > 0, or 1 according to whether the value > of expression is negative, zero or > positive.

My Version

In short:

this.compareTo(that)

returns

  • a negative int if this < that
  • 0 if this == that
  • a positive int if this > that

where the implementation of this method determines the actual semantics of < > and == (I don't mean == in the sense of java's object identity operator)

Examples

"abc".compareTo("def")

will yield something smaller than 0 as abc is alphabetically before def.

Integer.valueOf(2).compareTo(Integer.valueOf(1))

will yield something larger than 0 because 2 is larger than 1.

Some additional points

Note: It is good practice for a class that implements Comparable to declare the semantics of it's compareTo() method in the javadocs.

Note: you should read at least one of the following:

Warning: you should never rely on the return values of compareTo being -1, 0 and 1. You should always test for x < 0, x == 0, x > 0, respectively.

Solution 2 - Java

I use this mnemonic :

a.compareTo(b) < 0 // a < b

a.compareTo(b) > 0 // a > b

a.compareTo(b) == 0 // a == b

You keep the signs and always compare the result of compareTo() to 0

Solution 3 - Java

Answer in short: (search your situation)

  • 1.compareTo(0) (return: 1)
  • 1.compareTo(1) (return: 0)
  • 0.comapreTo(1) (return: -1)

Solution 4 - Java

take example if we want to compare "a" and "b", i.e ("a" == this)

  1. negative int if a < b
  2. if a == b
  3. Positive int if a > b

Solution 5 - Java

It can be used for sorting, and 0 means "equal" while -1, and 1 means "less" and "more (greater)".

Any return value that is less than 0 means that left operand is lesser, and if value is bigger than 0 then left operand is bigger.

Solution 6 - Java

int x = thisObject.compareTo(anotherObject);

The compareTo() method returns an int with the following characteristics:

  • negative If thisObject < anotherObject
  • zero If thisObject == anotherObject
  • positive If thisObject > anotherObject

Solution 7 - Java

System.out.println(A.compareTo(B)>0?"Yes":"No")

if the value of A>B it will return "Yes" or "No".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMagggiView Question on Stackoverflow
Solution 1 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 2 - JavaColin HebertView Answer on Stackoverflow
Solution 3 - Javauser1012506View Answer on Stackoverflow
Solution 4 - JavabhaveshView Answer on Stackoverflow
Solution 5 - JavaMichał NiklasView Answer on Stackoverflow
Solution 6 - JavaArtur YolchyanView Answer on Stackoverflow
Solution 7 - JavaChaitanya VibhuView Answer on Stackoverflow