The API writes:
The public int compareTo(string anotherstring) compares two strings in dictionary order. The comparison is based on the Unicode value of each character in the string. The sequence of characters represented by this string object in dictionary order is compared to the sequence of characters represented by the parameter string. If the string object is in dictionary order before the parameter string, the comparison results in a negative integer. If the string object is in dictionary order after the parameter string, the comparison results in a positive integer. If the two strings are equal, the result is 0; CompareTo returns 0 only if the method equals (Object) returns True.
This is the definition of dictionary sorting. If the two strings are different, they either have different characters at an index (the index is a valid index for both), either at different lengths or both. If they have different characters at one or more index locations, assuming K is the minimum value for such an index, then the string with a lesser value on position K (determined by the < operator) has a dictionary order that precedes the other strings. In this case, CompareTo returns the difference between the two char values of the two strings at position K, which is the value:
This.charat (k)-anotherstring.charat (k) If there are no different index positions for characters, the dictionary order of shorter strings is preceded by a longer string. In this case, COMPARETO returns the difference between the lengths of the two strings, which is the value:
This.length ()-anotherstring.length ()
specified by: compareTo parameter in interface comparable<string> : anotherstring-String to compare. returns a value of 0 if the argument string is equal to this string, and returns a value less than 0 if the string is less than the string parameter in dictionary order, or a value greater than 0 if the string is greater than the string parameter in dictionary order.
Let's say for example:
Package Go.derek;
public class Comparato {public
static void Main (string[] args) {
String x1= "a";
String x2= "C";
The ASCII value of a is 2 smaller than C, so output-2
System.out.println (X1.compareto (x2));
String x3= "abc";
String x4= "AB";
The length of the ABC is 1 greater than the length of AB, so the output is 1
System.out.println (X3.compareto (x4));
String x5= "Cat";
String x6= "a";
The cat and the A are different in the subscript index 1, the ASCII value of a is smaller than H 7, so output-7
System.out.println (X6));
String x7= "a";
String x8= "a";
Ignores case differences, so the output is 0
System.out.println (x7.comparetoignorecase (x8));
}