Package jmri.util

Class AlphanumComparator

  • All Implemented Interfaces:
    Direct Known Subclasses:
    CVNameComparator, PreferNumericComparator

    public class AlphanumComparator
    extends java.lang.Object
    implements java.util.Comparator<java.lang.String>
    This is an updated version with enhancements made by Daniel Migowski, Andre Bogus, and David Koelle

    To use this class: Use the static "sort" method from the java.util.Collections class: Collections.sort(your list, new AlphanumComparator());

    Note: this code compares numbers one at a time if those numbers are in chunks of the same size. For example, when comparing abc123 to abc184, 123 and 184 are the same size, so their values are compared digit-by- digit: 1 equals 1, 2 is less than 8, etc. This was done to solve the problem of numeric chunks that are too large to fit in range of values allowed by the programming language for a particular datatype: in Java, an int is limited to 2147483647. The problem with this approach is doesn't properly handle numbers that have leading zeros. For example, 0001 is seem as larger than 1 because it's the longer number. A version that does not compare leading zeros is forthcoming.

    • Field Summary

      Modifier and Type Field Description
      (package private) int skip  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.String s1, java.lang.String s2)  
      • 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
    • Method Detail

      • compare

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