Class NaturalStringOrder


  • public class NaturalStringOrder
    extends Object
    Utility class for natural string order operations (a1 < a2 < a10). Written by Stephen Friedrich (http://weblogs.java.net/blog/skelvin/archive/2006/01/natural_string.html). No license specified? Code found at http://svn.atlassian.com/fisheye/browse/public /contrib/confluence/linking-plugin /trunk/src/java/com/eekboom/utils/Strings.java?r=1322&%40annotateMode=none TODO: Determine license situation
    • Method Detail

      • compareNatural

        public static int compareNatural​(String s,
                                         String t)
        Returns a comparator that compares contained numbers based on their numeric values and compares other parts using the current locale's order rules.

        For example in German locale this will be a comparator that handles umlauts correctly and ignores upper/lower case differences.

        Parameters:
        collator - used for locale specific comparison of text (non-number) subwords - must not be null
        s - first string
        t - second string
        Returns:

        A string comparator that uses the current locale's order rules and handles embedded numbers correctly.

        See Also:
        / public static Comparator getNaturalComparator() { Collator collator = Collator.getInstance(); return getNaturalComparator(collator); } /** Returns a comparator that compares contained numbers based on their numeric values and compares other parts using the given collator., / public static Comparator getNaturalComparator(final Collator collator) { if(collator == null) { // it's important to explicitly handle this here - else the bug will manifest anytime later in possibly // unrelated code that tries to use the comparator throw new NullPointerException("collator must not be null"); } return new Comparator() { public int compare(String o1, String o2) { return compareNatural(collator, o1, o2); } }; } /** Returns a comparator that compares contained numbers based on their numeric values and compares other parts based on each character's Unicode value., #getNaturalComparator(), / public static Comparator getNaturalComparatorAscii() { return NATURAL_COMPARATOR_ASCII; } /** Returns a comparator that compares contained numbers based on their numeric values and compares other parts based on each character's Unicode value while ignore upper/lower case differences. Do not use if your app might ever run on any locale that uses more than 7-bit ascii characters., #getNaturalComparator(), / public static Comparator getNaturalComparatorIgnoreCaseAscii() { return IGNORE_CASE_NATURAL_COMPARATOR_ASCII; } /** Compares two strings using the current locale's rules and comparing contained numbers based on their numeric values. This is probably the best default comparison to use. If you know that the texts to be compared are in a certain language that differs from the default locale's langage, then get a collator for the desired locale ( ) and pass it to
      • compareNatural

        public static int compareNatural​(Collator collator,
                                         String s,
                                         String t)

        Compares two strings using the given collator and comparing contained numbers based on their numeric values.

        Parameters:
        s - first string
        t - second string
        Returns:
        zero iff s and t are equal, a value less than zero iff s lexicographically precedes t and a value larger than zero iff s lexicographically follows t
      • compareNaturalAscii

        public static int compareNaturalAscii​(String s,
                                              String t)

        Compares two strings using each character's Unicode value for non-digit characters and the numeric values off any contained numbers.

        (This will probably make sense only for strings containing 7-bit ascii characters only.)

        Returns:
        zero iff s and t are equal, a value less than zero iff s lexicographically precedes t and a value larger than zero iff s lexicographically follows t
      • compareNaturalIgnoreCaseAscii

        public static int compareNaturalIgnoreCaseAscii​(String s,
                                                        String t)

        Compares two strings using each character's Unicode value - ignoring upper/lower case - for non-digit characters and the numeric values of any contained numbers.

        (This will probably make sense only for strings containing 7-bit ascii characters only.)

        Returns:
        zero iff s and t are equal, a value less than zero iff s lexicographically precedes t and a value larger than zero iff s lexicographically follows t