Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Numeric Comparator

 
Jason Ferguson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21200
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Integer class has a parseInt() method that you may find helpful.
 
Jason Ferguson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic