Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Natural sort order string comparison in Java - is one built in? [duplicate]

I'd like some kind of string comparison function that preserves natural sort order1. Is there anything like this built into Java? I can't find anything in the String class, and the Comparator class only knows of two implementations.

I can roll my own (it's not a very hard problem), but I'd rather not re-invent the wheel if I don't have to.

In my specific case, I have software version strings that I want to sort. So I want "1.2.10.5" to be considered greater than "1.2.9.1".


1 By "natural" sort order, I mean it compares strings the way a human would compare them, as opposed to "ascii-betical" sort ordering that only makes sense to programmers. In other words, "image9.jpg" is less than "image10.jpg", and "album1set2page9photo1.jpg" is less than "album1set2page10photo5.jpg", and "1.2.9.1" is less than "1.2.10.5"

like image 573
Kip Avatar asked Aug 11 '09 18:08

Kip


People also ask

What is natural sorting order in java?

In Java, the Natural order is defined as the ordering provided by the JVM. This might not match what a people might believe is the natural order. e.g. Strings are sorted ASCIIbetically. Meaning an uppercase Z comes before a lowercase a and 10 is before 2.

What is the natural sorting order?

In computing, natural sort order (or natural sorting) is the ordering of strings in alphabetical order, except that multi-digit numbers are treated atomically, i.e., as if they were a single character.


2 Answers

In java the "natural" order meaning is "lexicographical" order, so there is no implementation in the core like the one you're looking for.

There are open source implementations.

Here's one:

NaturalOrderComparator.java

Make sure you read the:

Cougaar Open Source License

I hope this helps!

like image 147
OscarRyz Avatar answered Sep 19 '22 11:09

OscarRyz


I have tested three Java implementations mentioned here by others and found that their work slightly differently but none as I would expect.

Both AlphaNumericStringComparator and AlphanumComparator do not ignore whitespaces so that pic2 is placed before pic 1.

On the other hand NaturalOrderComparator ignores not only whitespaces but also all leading zeros so that sig[1] precedes sig[0].

Regarding performance AlphaNumericStringComparator is ~x10 slower then the other two.

like image 35
Mikhail Poda Avatar answered Sep 19 '22 11:09

Mikhail Poda