Package jmri.util

Class AlphanumComparator

java.lang.Object
jmri.util.AlphanumComparator
All Implemented Interfaces:
Comparator<String>
Direct Known Subclasses:
CVNameComparator, PreferNumericComparator

public class AlphanumComparator extends Object implements Comparator<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.