Class AlphanumComparator

  • All Implemented Interfaces:
    Comparator<CharSequence>

    public class AlphanumComparator
    extends Object
    implements Comparator<CharSequence>

    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.

    Author:
    Michel Kraemer