3

So, I'm trying to compare two String in Java right now. But the compareTo() method works weird. Consider this example:

String one = "one";
String two = "this is muuch greater";
System.out.println(one.compareTo(two));

And if I try to compare them the method works just fine. It returns a negative value.

But if I try something different, for example: (1)

String one = "word";
String two = "hello world";
System.out.println(one.compareTo(two));

or something like this: (2)

String one = "key";
String two = "qw";
System.out.println(one.compareTo(two));

It acts all weird. In the (1) case the method returned a positive value, despite String one being shorter than the String two

In the (2) case the method returned a negative value, despite the first string being longer than the other one.

It says, that the compareTo() method returns a positive value if the string to which the method is "applied" is longer than the string passing into arguments. The method returns 0 if their length is equal and returns a negative value in every other case. What is wrong am i doing?

Neykuratick
  • 127
  • 4
  • 14
  • 6
    String comparison uses lexicographical semantics, just as a dictionary, where *"all"* will be found before *"comprehensive"* and *"comprehensive"* before *"dog"*: not much to do with length. If you need it to compare lengths, you need to implement your own comparator, and in fact it would be something as simple as `one.lengt() < two.length()` – ernest_k Apr 15 '21 at 06:08
  • 3
    "It says, that the compareTo() method returns a positive value..." _What_ says? _Who_ says? – Sweeper Apr 15 '21 at 06:11
  • 1
    To add to @ernest_k explanation: `Integer.compare(one.length(), two.length())` being `Integer.signum(one.length() - two.length())` – Joop Eggen Apr 15 '21 at 06:20
  • Your whole question is based on a flawed premise. Dont ASSUME what a library method does. Read its javadoc. – GhostCat Apr 15 '21 at 07:47

2 Answers2

3

compareTo() method compares the given string with current string lexicographically. It returns positive number, negative number or 0.

It compares strings on the basis of Unicode value of each character in the strings.

If first string is lexicographically greater than second string, it returns positive number (difference of character value). If first string is less than second string lexicographically, it returns negative number and if first string is lexicographically equal to second string, it returns 0.

samabcde
  • 6,988
  • 2
  • 25
  • 41
0

If you get source code you see that compareTo in String compares subsequentelly Unicode of characters in strings till one of string is finished. At first mismatch if stops and returns difference between char codes in first and second strings. If no mismatch found method returns lengths difference between first and second strings.

So, 'o' code is smaller than 't' code (difference is negative), 'w' code greater than 'h' and 'k' code greater than 'q' (difference is positive). In this case code grows in alphabetic order.

public int compareTo(String anotherString) {
    int len1 = value.length;
    int len2 = anotherString.value.length;
    int lim = Math.min(len1, len2);
    char v1[] = value;
    char v2[] = anotherString.value;

    int k = 0;
    while (k < lim) {
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}
Sergey Afinogenov
  • 2,137
  • 4
  • 13