In the documentation of compareTo
function, I read:
Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.
What does this less than
or greater than
mean in the context of strings? Is -for example- Hello World
less than a single character a
?
val epicString = "Hello World"
println(epicString.compareTo("a")) //-25
Why -25
and not -10
or -1
(for example)?
Other examples:
val epicString = "Hello World"
println(epicString.compareTo("HelloWorld")) //-55
Is Hello World
less than HelloWorld
? Why?
Why it returns -55
and not -1
, -2
, -3
, etc?
val epicString = "Hello World"
println(epicString.compareTo("Hello World")) //55
Is Hello World
greater than Hello World
? Why?
Why it returns 55
and not 1
, 2
, 3
, etc?
I believe you're asking about the implementation of compareTo
method for java.lang.String
. Here is a source code for java 11:
public int compareTo(String anotherString) {
byte v1[] = value;
byte v2[] = anotherString.value;
if (coder() == anotherString.coder()) {
return isLatin1() ? StringLatin1.compareTo(v1, v2)
: StringUTF16.compareTo(v1, v2);
}
return isLatin1() ? StringLatin1.compareToUTF16(v1, v2)
: StringUTF16.compareToLatin1(v1, v2);
}
So we have a delegation to either StringLatin1
or StringUTF16
here, so we should look further:
Fortunately StringLatin1
and StringUTF16
have similar implementation when it comes to compare
functionality:
Here is an implementation for StringLatin1
for example:
public static int compareTo(byte[] value, byte[] other) {
int len1 = value.length;
int len2 = other.length;
return compareTo(value, other, len1, len2);
}
public static int compareTo(byte[] value, byte[] other, int len1, int len2) {
int lim = Math.min(len1, len2);
for (int k = 0; k < lim; k++) {
if (value[k] != other[k]) {
return getChar(value, k) - getChar(other, k);
}
}
return len1 - len2;
}
As you see, it iterated over the characters of the shorter string and in case the charaters in the same index of two strings are different it returns the difference between them. If during the iterations it doesn't find any different (one string is prefix of another) it resorts to the comparison between the length of two strings.
In your case, there is a difference in the first iteration already...
So its the same as `"H".compareTo("a") --> -25".
The code of "H" is 72
The code of "a" is 97
So, 72 - 97 = -25