How does compareTo work?
Asked Answered
A

3

12

I know that compareTo returns a negative or positive result on how well one string correlates to the other, but then why:

public class Test {
    public static void main(String[] args) {
        String y = "ab2";
        if(y.compareTo("ac3") == -1) {
            System.out.println("Test");
        }
    }
}

is true and

public class Test {
    public static void main(String[] args) {
        String y = "ab2";
        if(y.compareTo("ab3") == -1) {
            System.out.println("Test");
        }
    }
}

is also true?

Adler answered 7/9, 2015 at 17:13 Comment(2)
Don't use ==-1 but <0. Notice that "ab2".compareTo("cb2") returns -2, just like "ab2".compareTo("ab4") (result is difference in Unicode Table of character which are different in string indexes - if length of strings is same).Philipp
Comparison using compareTo does not use correlation: correlation is a symmetric property, whereas compareTo is (or at least should be) anti-symmetric, at least in the sense that sign(a.compareTo(b)) = -sign(b.compareTo(a)).Asterism
K
19

The general contract of Comparable.compareTo(o) is to return

  • a positive integer if this is greater than the other object.
  • a negative integer if this is lower than the other object.
  • 0 if this is equals to the other object.

In your example "ab2".compareTo("ac3") == -1 and "ab2".compareTo("ab3") == -1 only means that "ab2" is lower than both "ac3" and "ab3". You cannot conclude anything regarding "ac3" and "ab3" with only these examples.

This result is expected since b comes before c in the alphabet (so "ab2" < "ac3") and 2 comes before 3 (so "ab2" < "ab3"): Java sorts Strings lexicographically.

Kief answered 7/9, 2015 at 17:16 Comment(1)
It is important to know that "ab7" would still be smaller than "ac2" even though the last integer suggests ab7 should be greater than ac2 because the compareTo() lexicographically works as follows:- If Strings are different then take the least index where it differs in our example "ab7" and "ac2" the least index is 1(even though index 2 also differs!) and in that index see the lexicographical order (b is smaller than c) hence "ab7" is smaller to "ac2".Diagenesis
A
1

compareTo for Strings returns -1 if the first String (the one for which the method is called) comes before the second String (the method's argument) in lexicographical order. "ab2" comes before "ab3" (since the first two characters are equal and 2 comes before 3) and also before "ac3" (since the first character is equal and b comes before c), so both comparisons return -1.

Auberbach answered 7/9, 2015 at 17:16 Comment(1)
More precisely: compareTo for Strings returns a negative number if the first String comes before the second StringKief
A
1

compareTo() compares two string with regard to their alphabetical order. Both of you tests have a String that is alphabetically sorted "before" the String you compare it with. In other words:

  • ab2 < ac3 (because b < c)
  • ab2 < ab3 (because 2 < 3)

By the way, you should rather use "< 0" than "== -1" in your if statement, as the compareTo spec says that the function returns a negative number, not specifically "-1"

Ashling answered 7/9, 2015 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.