-2

I have a quick question about comparing two Strings. these are my Strings:

String s1 = "bc";
String s2 = "bb";

My understanding is that s1 < s2 because c comes after b in the alphabet. So s1.compareTo(s2) should return -1. How ever if I test this with System.out.println(s1.compareTo(s2)); it return 1. So what did I understand wrong?

JcodingW
  • 27
  • 3
  • Possible duplicate of [Alphabet constant in Java?](https://stackoverflow.com/questions/6205771/alphabet-constant-in-java) – Squareoot Jan 13 '18 at 10:02
  • Possible duplicate of [How to use the Comparable CompareTo on Strings in Java](https://stackoverflow.com/questions/3748805/how-to-use-the-comparable-compareto-on-strings-in-java) – vinS Jan 13 '18 at 10:03
  • 3
    "c comes after b" => Well ... That means that `s1` is _greater_ than `s2`! – Seelenvirtuose Jan 13 '18 at 10:04
  • Your statement is about equivalent to saying 23 < 22 (which is obviously not true) because 3 comes after 2 (which is true). Is that just a lapse of judgement on your part or is there a specific reason why you believe one implies the other? – Bernhard Barker Jan 13 '18 at 10:08
  • 3
    "So what did I understand wrong?" basically this part: "My understanding is that s1 < s2 because c comes after b in the alphabet." – Kayaman Jan 13 '18 at 10:09

6 Answers6

3

You seem to have misunderstood what is meant by "lexicographic order".

Since c comes after b, s1 is considered bigger than s2!

Think of letters as numbers. a is 1, b is 2, c is 3, and so on. 2 comes after 1, so 2 is bigger than 1.

So comparing bb and bc is just like comparing 22 and 23. Obviously, 23 is bigger.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
2

Assuming below is the below program as per your code snippet:

package devsought;

public class JavaStringCompareTo {

    public static void main(String... args) {
        String s1="bc";
        String s2="bb";
        System.out.println(s1.compareTo(s2));
    }
}

The output is 1

Explanation:

b (index 0) c (index 1)
b (index 0) b (index 1)

At index 1(second position),we have two characters ‘c’(unicode value \u0063,Decimal value 99) and ‘b’(unicode value \u0062 ,Decimal value 98).The difference 99-98=1 is returned and printed on the console. Check out for more examples on java String compareTo method

John Kyalo
  • 31
  • 3
0

c has a greater index in the ASCII table (cast char 'c' and char 'b' into integer) c has the constant 99 and b 98, so S1 is bigger than S2

Pfanta
  • 1
  • 1
  • 2
0

from the doc

returned value:

the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

Robdll
  • 5,865
  • 7
  • 31
  • 51
0

My understanding is that s1 < s2 because c comes after b in the alphabet.

It works in the other way.
For String objects, compareTo() works in this way :

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal;

Lexicographically or more simply for a case with only alphabetic characters, in a dictionary a "bc" word would follow a "bb" word.
So "bc".compareTo("bb") returns a positive value according to the specification.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

Have a look at java doc of String's compareTo method here

It says

The comparison is based on the Unicode value of each character in the strings. The character sequence represented by thisString object is compared lexicographically to the character sequence represented by the argument string. 

Which is evaluated as follows this.charAt(k)-anotherString.charAt(k)

Since Unicode value of character c is higher than that of character b result is 1. Which is what you are seeing.

Have a look at Unicode list

Hope this helps

Nishant
  • 222
  • 6
  • 21