Class AlphaNumComparator<T>

java.lang.Object
uk.ac.ebi.utils.collections.AlphaNumComparator<T>
All Implemented Interfaces:
Comparator<T>

public class AlphaNumComparator<T> extends Object implements Comparator<T>
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 http://www.DaveKoelle.com

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

To convert to use Templates (Java 1.5+): - Change "implements Comparator" to "implements Comparator" - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)" - Remove the type checking and casting in compare().

To use this class: for example, use the static "sort" method from the Collections class: Collections.sort(your list, new AlphanumComparator()); See AlphaNumComparatorTest for examples.

Notes

  • null is considered lower than anything else.
  • "Item 3" comes before "Item 01", when you have zero-padding, it is taken into account.
  • Constructor Details

    • AlphaNumComparator

      public AlphaNumComparator(boolean isCaseSensitive)
      Default is true
    • AlphaNumComparator

      public AlphaNumComparator()
      Default is true
  • Method Details