Natural sort order

P

Paul

I have a field that contains both numeric and alphanumeric entries, so
e.g. when I do an 'order by' in sql I would get the following order

1.2, 1.25, 3, 50, 6, ALMERA, FOCUS

But in this case , the 6 should be above the 50, but the order by
looks at the first digit only.

To get around this, I used the java.util.comparator implementations
that i found on the web at http://www.davekoelle.com/alphanum.jsp and
http://sourcefrog.net/projects/natsort/ -
http://pierre-luc.paour.9online.fr/NaturalOrderComparator.java ,
however both of these fall down when there are decimal points involved
i.e. I'm getting

'1.2', '1.3', '1.25'

when I should get
'1.2', '1.25', '1.3'

Because they compare the '3' with '25' and find '25' to be bigger.
Does anyone know of a solution to this ?
 
S

Stefan Schulz

Paul said:
'1.2', '1.3', '1.25'

when I should get
'1.2', '1.25', '1.3'

Because they compare the '3' with '25' and find '25' to be bigger.
Does anyone know of a solution to this ?

Only one suggestion: If you have a pure number, you can parse it by
useing Double.parseDouble(String), and then compare the resultant
doubles. If that fails, you have a "normal" String, and can use the
default string Comparison. Now all you need to do is make all doubles
smaller then any String.

Implementing that in a Comparator is left to the reader ;)

See you
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top