Class AlphaNumericComparator

  • All Implemented Interfaces:
    java.util.Comparator

    
    public class AlphaNumericComparator<T>
     implements Comparator<T>
                        

    Comparator for Objects, that compares based on their converted values. The objects will be converted to a String value using the given Function. That value will be compared in a human readable fashion by trying to parse numbers that appear in the keys as integers and compare those, too.

    Heavily influenced by https://codereview.stackexchange.com/questions/37192/number-aware-string-sorting-with-comparator

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      int compare(T o1, T o2)
      • Methods inherited from class java.util.Comparator

        comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reverseOrder, reversed, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AlphaNumericComparator

        AlphaNumericComparator(Function<in T, String> converter)
        Constructs a comparator with a converter function
        Parameters:
        converter - that generates a String value from the arguments given to compare