What is lexicographical comparison?
Lexicographical comparison is a technique used for sorting words alphabetically by comparing the characters in a string sequentially from left to right. The order of precedence used when comparing the characters in a string is:
1. Digits
2. Uppercase letters
3. Lowercase letters
In the following figure, we display how a lexicographical comparison works when comparing each set of strings:
Here, the box with the bold outline indicates the letter that decides the precedence when comparing these two strings. When doing a comparison, you start on the left side and go from left to right, one letter at a time, and compare the letters at that position. If they are the same, you move on to the next letter. However, if the letters do not match, you can decide on which letter is greater and at that point you have an answer for the lexicographical comparison.
The resulting alphabetic list is:
d2t2
Data
daTa
data