Class AlphanumComparator

java.lang.Object
net.okapia.osid.torrefacto.util.AlphanumComparator
All Implemented Interfaces:
Comparator<String>

public class AlphanumComparator extends Object implements Comparator<String>
Compares strings so that embedded runs of digits sort by numeric value rather than character by character: "z2" sorts before "z10", and "file9" before "file10". Each string is split into alternating chunks of digits and non-digits; digit chunks are compared as numbers and the remaining chunks are compared with a delegate comparator (natural string order by default). This is Dave Koelle's Alphanum algorithm; see http://www.DaveKoelle.com.
  • Constructor Details

    • AlphanumComparator

      public AlphanumComparator()
      Constructs an AlphanumComparator that orders non-numeric chunks in natural string order.
    • AlphanumComparator

      public AlphanumComparator(Comparator<String> comparator)
      Constructs an AlphanumComparator that orders non-numeric chunks with the given comparator.
      Parameters:
      comparator - the comparator for non-numeric chunks
  • Method Details

    • compare

      public int compare(String s1, String s2)
      Compares two strings by the alphanum rule.
      Specified by:
      compare in interface Comparator<String>
      Parameters:
      s1 - the first string
      s2 - the second string
      Returns:
      a negative integer, zero, or a positive integer as s1 sorts before, equal to, or after s2