Natural sort order string comparison in Java - is one built in?
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!
Compare strings by natural order but ignoring string's prefix
You're answering your own question: With a comparator.
Comparator<String> marcosPrefixIgnoringComparison =
(a, b) -> a.substring(4).compareTo(b.substring(4));
That's assuming that the prefix is defined as 'the first 4 characters'. If it's more 'The string let
, and then any number of digits', you'd have to do something else. Possibly regexes:
Comparator<String> marcosPrefixIgnoringComparison =
(a, b) -> a.replaceFirst("^let\\d+\\s+", "").compareTo(
b.replaceFirst("^let\\d+\\s+", ""));
your question is not particularly clear about what 'prefix' means, here.
Revert to natural sorted order if string length is equal
Just put an if else block in your compare method.
Pseudocode:
if lengths are not equal
return o1's length compared to o2's length.
else
return o1 compared to o2.
I think that you can figure out the rest.
Java String Number Comparator
There is an article about this on Coding Horror. This is called natural sorting, where you effectively treat a group of digits as a single "character". See this question for some Java implementations of the idea.
Sorting for Humans : Natural Sort Order
The default sort functions in almost every programming language are poorly suited for human consumption. What do I mean by that? Well, consider the difference between sorting filenames in Windows explorer, and sorting those very same filenames via
Array.Sort()
code:
continued...
Sorting ArrayList with natural sort order in Android?
Although not a good solution but you will get an idea, Try this:
Collections.sort(mArrayList, new Comparator<String>() {
public int compare(String s1, String s2) {
Integer i1 = Integer.parseInt(s1.split("test ")[1]);
Integer i2 = Integer.parseInt(s2.split("test ")[1]);
return i1.compareTo(i2);
});
Basically you can sort any object. So I recommend having a Data
class containing all your variables then you can sort these objects on any variable's basis. So e.g.
public class Data{
public String test;
public Integer i;
}
ArrayList<Data> mArrayList = new ArrayList<Data>();
.
.
.
Collections.sort(mArrayList, new Comparator<Data>() {
public int compare(Data d1, Data d2) {
return d1.i.compareTo(d2.i);
});
Is there a Comparator to naturally sort strings that may contain numbers, guava?
No, there isn't any built in implementation of such a comparator in Guava... seems like something that's too narrowly applicable to me. Just write your own implementation or use one of the existing ones that have been linked already.
difference between natural ordering and total ordering
Total ordering means all values can be compared to all other values. For example, if you have a collection of BigDecimal
and String
there is no natural total order (but you could invent one)
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
http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Sort on a string that may contain a number
The Alphanum Algorithm
From the website
"People sort strings with numbers differently than software. Most sorting algorithms compare ASCII values, which produces an ordering that is inconsistent with human logic. Here's how to fix it."
Edit: Here's a link to the Java Comparator Implementation from that site.
Related Topics
What Is a Question Mark "" and Colon ":" Operator Used For
How to Validate Two or More Fields in Combination
What Is a Good Use Case for Static Import of Methods
Mapping Postgresql JSON Column to a Hibernate Entity Property
Creating Multiple Log Files of Different Content with Log4J
Jfreechart Scaling of Boxplots with Several Categories
What Should I Set Java_Home Environment Variable on MACos X 10.6
Overload with Different Return Type in Java
Eclipse: How to Build an Executable Jar with External Jar
Stale Element Reference: Element Is Not Attached to the Page Document
Why Do These Two Multiplication Operations Give Different Results
Java - Class.Getresource Returns Null
How to Use a Tablename Variable for a Java Prepared Statement Insert
Why am I Getting Java.Lang.Illegalstateexception "Not on Fx Application Thread" on Javafx
Why Is Stringtokenizer Deprecated