static class VersionsComparator.AlphaDecimalComparator
The returned comparator compares two character sequences as though each of them would be first transformed into a tuple of the form:
(A0, N0, A1, N1, ..., An-1, Nn-1, An, Nn)
where:
A0 and An are (possibly empty) sub-sequences
consisting of non-decimal-digit characters,
A1 ... An-1 are non-empty sub-sequences consisting of
non-decimal-digit characters,
N0 ... Nn-1 are non-empty sub-sequences consisting of
decimal-digit characters, and
Nn is a (possibly empty) sub-sequence consisting of
decimal-digit characters.
All sub-sequences concatenated together in order as they appear in the
tuple yield the original character sequence.
After transformation, the tuples are compared by their elements (from
left to right) so that corresponding Ax elements are compared
using the provided comparator alphaComparator and Nx
elements are compared as non negative decimal integers.
The first pair of compared elements that is different with respect to the
used comparator (either alphaComparator, or special decimal
comparator) if any, provides the result produced by this comparator.
The arguments are treated equal, if and only if all the subsequences,
both decimal and non-decimal, compare equal.
For example, the following array was sorted using such comparator:
{ "1ab", "5ab", "10ab",
"a1b", "a5b", "a10b",
"ab1", "ab5", "ab10" ;}
When comparing numerical parts, an empty character sequence is considered less than any non-empty sequence of decimal digits.
If the numeric values of two compared character sub-sequences are
equal, but their string representations have different number of leading
zeroes, the comparator treats the number with less leading zeros as
smaller.
For example, "abc 1" < "abc 01" < "abc 001".
String based on
case-insensitive ordering, and treating numbers with more leading
zeroes as greater, one could use
Comparator<String> cmp = Comparator.comparingAlphaDecimal(
Comparator.comparing(CharSequence::toString,
String::compareToIgnoreCase));
10 to determine the numeric
value of a digit represented by the given code point.alphaComparator - the comparator that compares sub-sequences
consisting of non-decimal-digits - the type of elements to be compared; normally
java.lang.CharSequence| Constructor and description |
|---|
VersionsComparator.AlphaDecimalComparator
(java.util.Comparator<? super java.lang.CharSequence> alphaComparator, boolean leadingZeroesFirst) |
| Methods inherited from class | Name |
|---|---|
class java.lang.Object |
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |