• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Numeric Comparator

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic