The essence of any comparator is to return the integer result of the comparison of objects. By holding down the Ctrl key and clicking on the method in most IDEs, you can proceed to the implementation of the method that interests us. This is how the string comparator works:
(strings are already cast to byte[] arrays)
@HotSpotIntrinsicCandidate public static int compareTo(byte[] value, byte[] other) { int len1 = value.length; int len2 = other.length; 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; }
He element-wise compares the characters from the first to the end of the shortest line. If there is a pair of different characters, then the result of comparing the first such pair is returned. If all characters match, then the result of comparing string lengths is returned.