This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes String Numeric Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Numeric Comparator" Watch "String Numeric Comparator" New topic
Author

String Numeric Comparator

Jason Ferguson
Ranch Hand

Joined: Sep 16, 2007
Posts: 47
Does anyone have a Comparator implementation that can sort String numbers correctly?

Example: the list

1,2,11,8,7,12

Should sort to:

1,2,7,8,11,12

Instead of:

1,11,12,2,7,8

Jason
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

Does anyone have a Comparator implementation that can sort String numbers correctly?


It shouldn't be that hard to write one... Just create a Comparator that takes strings, whose compare() method, converts the string to either int, float, or double, and then route the request to the Integer, Float, or Double class as the comparator.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The Integer class has a parseInt() method that you may find helpful.


"I'm not back." - Bill Harding, Twister
Jason Ferguson
Ranch Hand

Joined: Sep 16, 2007
Posts: 47
Originally posted by Henry Wong:


It shouldn't be that hard to write one... Just create a Comparator that takes strings, whose compare() method, converts the string to either int, float, or double, and then route the request to the Integer, Float, or Double class as the comparator.

Henry


I hate when I get in a hurry and leave one of the most critical parts out.

Suppose you are sorting addresses:
- 1 Main St
- 7 Main St
- 9 Main St
- 11 Main St
- 30 Main St

A simple string sort would turn this into:
- 1 Main St
- 11 Main St
- 30 Main St
- 7 Main St
- 9 Main St

So what we have here is a two part comparison: the number and the string. The number is the more important part, but after the number comes the text part of the string.

Is there a better way than splitting the string (probably via regexp)?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Splitting the string with a regex is the first thing I would try, and probably the last - unless profiling revealed that this particular bit of code needed to be further optimized for speed. In the latter case, I might use String.indexOf(' ') to locate the first space, rather than a regex. And use substring() to isolate the numeric part.

But if you're sorting addresses, is it more useful to sort them like this:


1 Aspen St.
1 Birch St.
1 Cedar Rd.
2 Aspen St.
2 Birch St.
2 Cedar Rd.
3 Aspen St.
3 Birch St.
3 Cedar Rd.

or like this:


1 Aspen St.
2 Aspen St.
3 Aspen St.
1 Birch St.
2 Birch St.
3 Birch St.
1 Cedar Rd.
2 Cedar Rd.
3 Cedar Rd.

I don't think there's any one definitive answer here, but it's something to consider.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

If this is for a real world project, keep in mind that addresses have many formats. For example, in my community an address is of the form "123-45 67th Street". Many apps refuse to handle dashes, which turns this into "123 45 67th Street". This wreaks havoc on many date algorithms because they are hard coded to assume the second "thing" is the street and it goes to 45th street - a mile away from me.

This is a pet peeve for me. Sometimes you can't enter a dash and then they complain your credit card address doesn't match (because the credit card company uses the correct address with a dash.) And that mail goes to the wrong place. (I do live in the US. It's a bit frustrating when other US companies make it so difficult to enter my address.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Numeric Comparator