What does "compare two strings lexicographically" mean?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
If you check which string would come first in a lexicon, you've done a lexicographical comparison of the strings!
Some links:
Stolen from the latter link:
Java lexicographically order:
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):
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.
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: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'
(21). Since this is a positive result, the parameter ("comparison"
) is lexicographically first.There is also a variant
.compareToIgnoreCase ()
which will return0
for"a".compareToIgnoreCase ("A");
for example.Comparing sequencially the letters that have the same position against each other.. more like how you order words in a dictionary
The
String.compareTo(..)
method performs lexicographical comparison. Lexicographically == alphebetically.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.