Class AlphanumComparator

java.lang.Object
de.gurkenlabs.litiengine.util.AlphanumComparator
All Implemented Interfaces:
Comparator<String>

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

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

  • Constructor Details

    • AlphanumComparator

      public AlphanumComparator()
  • Method Details

    • compare

      public int compare(String s1, String s2)
      Compares two strings using the Alphanum algorithm. This method compares two strings by breaking them into chunks of numeric and non-numeric characters and comparing these chunks numerically and lexicographically.
      Specified by:
      compare in interface Comparator<String>
      Parameters:
      s1 - the first string to compare
      s2 - the second string to compare
      Returns:
      a negative integer, zero, or a positive integer as the first string is less than, equal to, or greater than the second string
    • compareTo

      public static int compareTo(String s1, String s2)
      Compares two strings using the Alphanum algorithm. This method compares two strings by breaking them into chunks of numeric and non-numeric characters and comparing these chunks numerically and lexicographically.
      Parameters:
      s1 - the first string to compare
      s2 - the second string to compare
      Returns:
      a negative integer, zero, or a positive integer as the first string is less than, equal to, or greater than the second string