Performs locale-sensitive comparison of Strings with numbers
to produce a natural order that appears logical to humans.
Examples:
compare("Hello", "Hello") = 0
compare("10", "2") = 1
compare("10th", "2nd") = 1
compare("File 10", "File 2") = 1
compare("Manana", "Mañana") = -1
The implementation is loosely based on the Alphanum algorithm by
Dave Koelle and the
Java
implementation released under the MIT license. However, it has been
extended to use a Collator for locale-sensitive comparison, and it
is also able to compare arbitrarily large numbers.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public int | compare(CharSequence a, CharSequence b)
Implements java. Compares its two arguments for order. |
| private int | Returns: the index of the first digit of the number found or the length of the character sequence if no number was foundthe character sequence s, int the index of the character where the search should start start)Find the next number in the given character sequence |
| private int | Returns: the index of the first character that is not a digit or the length of the character sequence if there is no other characterthe character sequence s, int the index of the character where the search should start start)Find the end of a number in the given character sequence |
| collator | back to summary |
|---|---|
| private final Collator collator | |
| AlphanumComparator | back to summary |
|---|---|
| public AlphanumComparator(Locale locale) Create a new comparator for the given locale
| |
| compare | back to summary |
|---|---|
| public int compare(CharSequence a, CharSequence b) Implements java. Doc from java. Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
The implementor must ensure that
The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that
| |
| findNextNumber | back to summary |
|---|---|
| private int findNextNumber(CharSequence s, int start) Find the next number in the given character sequence
| |
| findNumberEnd | back to summary |
|---|---|
| private int findNumberEnd(CharSequence s, int start) Find the end of a number in the given character sequence
| |