Class AlphanumComparator

  • All Implemented Interfaces:
    java.util.Comparator<java.lang.CharSequence>

    public class AlphanumComparator
    extends java.lang.Object
    implements java.util.Comparator<java.lang.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
    • Constructor Summary

      Constructors 
      Constructor Description
      AlphanumComparator​(java.util.Locale locale)
      Create a new comparator for the given locale
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.CharSequence a, java.lang.CharSequence b)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Comparator

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Constructor Detail

      • AlphanumComparator

        public AlphanumComparator​(java.util.Locale locale)
        Create a new comparator for the given locale
        Parameters:
        locale - the locale
    • Method Detail

      • compare

        public int compare​(java.lang.CharSequence a,
                           java.lang.CharSequence b)
        Specified by:
        compare in interface java.util.Comparator<java.lang.CharSequence>