Java implementation of Alpha-numeric comparator

Xueming Shen xueming.shen at oracle.com
Tue Dec 16 19:32:39 UTC 2014


On 12/16/2014 10:44 AM, Ivan Gerasimov wrote:
>
> On 16.12.2014 17:58, roger riggs wrote:
>> Hi Ivan,
>>
>> In which package/class do you propose to add the API to get the comparator?
>>
> I was thinking of java.text package, though I don't see a specific class in which the static method could be naturally included.
> This kind of Comparator doesn't seem to be common enough to include in such a base interface as CharSequence.
>
>> In java.time, comparators are returned from static methods in an interface.
>> This allows lambda to be used for the implementation.
>> For example, ChronoZonedDateTime.timeLineOrder <https://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoZonedDateTime.html#timeLineOrder-->()[1]
>>
>> For example a static method could be added to CharSequence:
>> public static int Comparator<CharSequence> alphaNumericComparator() ...
>>
>> In use it would be CharSequence.alphaNumericComparator().compare("012", "234");
>>
>> 2)  Should there be any provision for number strings internal to the string with leading zeros.
>>   Should "abc-0123-def" be equal to "abc-123-def"?
>>
> The strings will be ordered as:
>
> abc-122-def
> abc-0122-def
> abc-123-def
> abc-0123-def
> abc-00123-def
> abc-0000123-def
> abc-124-def
>
> I.e. the strings with the same numeric value will be grouped together, but the strings with more leading zeros will be put in the order further.
> To my eyes the strings with more leading zeros look bigger, that's why I did it this way :)
>
> By the way, the Microsoft's StrCmpLogicalW() does it in the opposite direction, i.e. strings with more leading zeros come earlier.
> If people find it useful, we can make it configurable.
>

It's always desired to have a "customizable" approach for numeric order collation:-)

-Sherman



More information about the core-libs-dev mailing list