Class AlphanumComparator

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Comparator<java.lang.String>

    public final class AlphanumComparator
    extends java.lang.Object
    implements java.util.Comparator<java.lang.String>, java.io.Serializable
    The Alphanum Algorithm is an improved sorting algorithm for strings containing numbers: Instead of sorting numbers in ASCII order like a standard sort, this algorithm sorts numbers in numeric order.

    The Alphanum Algorithm is discussed at DaveKoelle.com

    This is an updated version with enhancements made by Daniel Migowski, Andre Bogus, David Koelle and others.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static byte[] ASCII_MAPPING
      A mapping from ASCII characters to the default Collator order.
      (package private) static java.lang.String ASCII_SORT_ORDER
      The sort order for the fast ASCII sort method.
      private static AlphanumComparator INSTANCE  
      private static long serialVersionUID  
      (package private) static boolean useFastASCIISort
      true to use the faster ASCII sorting algorithm.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private AlphanumComparator()
      Constructs a new Alphanum Comparator.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.String s1, java.lang.String s2)  
      private static int compareChunk​(java.lang.String thisChunk, int thisChunkLength, java.lang.String thatChunk, int thatChunkLength)
      Compare two string chunks
      private static int compareString​(java.lang.String string1, int len1, java.lang.String string2, int len2)
      Compare two ASCII strings in a manner compatible with the default Collator
      private static java.lang.String getChunk​(java.lang.String s, int slength, int marker)
      Returns an alphanum chunk.
      static AlphanumComparator getInstance()
      Replies the unique instance.
      private static boolean isAscii​(java.lang.String string, int stringLength)
      Check if a string is ASCII only
      • 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

    • Method Detail

      • compareString

        private static int compareString​(java.lang.String string1,
                                         int len1,
                                         java.lang.String string2,
                                         int len2)
        Compare two ASCII strings in a manner compatible with the default Collator
        Parameters:
        string1 - The first string to compare
        len1 - The length of the first string
        string2 - The second string to compare
        len2 - The length of the second string
        Returns:
        See String.compareToIgnoreCase(String) (e.g. string1.compareToIgnoreCase(string2)).
      • getChunk

        private static java.lang.String getChunk​(java.lang.String s,
                                                 int slength,
                                                 int marker)
        Returns an alphanum chunk. Length of string is passed in for improved efficiency (only need to calculate it once).
        Parameters:
        s - string
        slength - string length
        marker - position
        Returns:
        alphanum chunk found at given position
      • isAscii

        private static boolean isAscii​(java.lang.String string,
                                       int stringLength)
        Check if a string is ASCII only
        Parameters:
        string - The string to check
        stringLength - The length of the string (for performance reasons)
        Returns:
        true if the string only contains ascii characters
      • compareChunk

        private static int compareChunk​(java.lang.String thisChunk,
                                        int thisChunkLength,
                                        java.lang.String thatChunk,
                                        int thatChunkLength)
        Compare two string chunks
        Parameters:
        thisChunk - The first chunk to compare
        thisChunkLength - The length of the first chunk (for performance reasons)
        thatChunk - The second chunk to compare
        thatChunkLength - The length of the second chunk (for performance reasons)
        Returns:
        The Comparator result
      • compare

        public int compare​(java.lang.String s1,
                           java.lang.String s2)
        Specified by:
        compare in interface java.util.Comparator<java.lang.String>