Class AlphanumComparator

java.lang.Object
org.openstreetmap.josm.tools.AlphanumComparator
All Implemented Interfaces:
Serializable, Comparator<String>

public final class AlphanumComparator extends Object implements Comparator<String>, 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:
  • Field Details

  • Constructor Details

  • Method Details

    • getInstance

      Replies the unique instance.
      Returns:
      the unique instance
    • compareString

      private static int compareString(String string1, int len1, 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 String getChunk(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(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(String thisChunk, int thisChunkLength, 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(String s1, String s2)
      Specified by:
      compare in interface Comparator<String>