A string s precedes a string t in lexicographic order if
s is a prefix of t, or
if c and d are respectively the first character of s and t in which s and t differ, then c precedes d in character order.
Note: For the characters that are alphabetical letters, the character order coincides with the alphabetical order. Digits precede letters, and uppercase letters precede lowercase ones.
The wording "comparison" is mildly misleading. You are not comparing for strict equality but for which string comes first in the dictionary (lexicon).
This is the feature that allows collections of strings to be sortable.
Note that this is very dependent on the active locale. For instance, here in Denmark we have a character "å" which used to be spelled as "aa" and is very distinct from two single a's (EDIT: If pronounced as "å"!). Hence Danish sorting rules treat two consequtive a's identically to an "å", which means that it goes after z. This also means that Danish dictionaries are sorted differently than English or Swedish ones.
Leading from answers from @Bozho and @aioobe, lexicographic comparisons are similar to the ordering that one might find in a dictionary.
The Java String class provides the .compareTo () method in order to lexicographically compare Strings. It is used like this "apple".compareTo ("banana").
The return of this method is an int which can be interpreted as follows:
returns < 0 then the String calling the method is lexicographically first (comes first in a dictionary)
returns == 0 then the two strings are lexicographically equivalent
returns > 0 then the parameter passed to the compareTo method is lexicographically first.
More specifically, the method provides the first non-zero difference in ASCII values.
Thus "computer".compareTo ("comparison") will return a value of (int) 'u' - (int) 'a' (20). Since this is a positive result, the parameter ("comparison") is lexicographically first.
There is also a variant .compareToIgnoreCase () which will return 0 for "a".compareToIgnoreCase ("A"); for example.
Odd as this seems, it is true... I have had to write comparator chains to be able to change the default behavior. Play around with the following snippet with better examples of input strings to verify the order (you will need JSE 8):
import java.util.ArrayList;
public class HelloLambda {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Kambiz");
names.add("kambiz");
names.add("k1ambiz");
names.add("1Bmbiza");
names.add("Samantha");
names.add("Jakey");
names.add("Lesley");
names.add("Hayley");
names.add("Benjamin");
names.add("Anthony");
names.stream().
filter(e -> e.contains("a")).
sorted().
forEach(System.out::println);
}
}
Result
1Bmbiza
Benjamin
Hayley
Jakey
Kambiz
Samantha
k1ambiz
kambiz
Please note this is answer is Locale specific.
Please note that I am filtering for a name containing the lowercase letter a.
You might also come across a task, where you have to implement the lexicographical comparison "manually", not using the default compareTo() method.
The below simple algorithm is based on comparing the Unicode value of chars at subsequent positions.
@Override
public int compareTo(Person otherPerson) {
// Getters, constructor, variables ...
int result = 0;
for (int i = 0; i < getName().length() && i < otherPerson.getName().length(); i++) {
if (getName().charAt(i) > otherPerson.getName().charAt(i)) {
result = 1;
break;
} else if (getName().charAt(i) < otherPerson.getName().charAt(i)) {
result = -1;
break;
}
}
}
return result;
}
}