Natural string sorting

Discussion in 'Java' started by sks, Jun 18, 2004.

  1. sks

    sks Guest

    Anyone got any advice on how to get a list of String's to sort 'naturally'.
    What I mean is, take this 'sorted' list of 'hard drive capacities':

    120GB
    160GB
    200GB
    20GB
    250GB
    40GB
    80GB

    Obviously to a human, it would be sorted as such

    20GB
    40GB
    80GB
    120GB
    160GB
    200GB
    250GB
    sks, Jun 18, 2004
    #1
    1. Advertising

  2. sks

    Jan Schaefer Guest

    sks schrieb:
    > Anyone got any advice on how to get a list of String's to sort 'naturally'.
    > What I mean is, take this 'sorted' list of 'hard drive capacities':
    > Obviously to a human, it would be sorted as such
    >
    > 20GB
    > 40GB
    > 80GB


    The only solution that comes to my mind is to parse the Strings an cast
    the numbers to Integer for sorting.

    Just my two eurocents,

    Jan
    Jan Schaefer, Jun 18, 2004
    #2
    1. Advertising

  3. sks

    VisionSet Guest

    "sks" <11.co.uk> wrote in message
    news:cauru0$g7$...
    > Anyone got any advice on how to get a list of String's to sort

    'naturally'.
    > What I mean is, take this 'sorted' list of 'hard drive capacities':
    >
    > 120GB
    > 160GB
    > 200GB
    > 20GB
    > 250GB
    > 40GB
    > 80GB
    >
    > Obviously to a human, it would be sorted as such
    >
    > 20GB
    > 40GB
    > 80GB
    > 120GB
    > 160GB
    > 200GB
    > 250GB



    Write a java.util.Comparator and offer this to a sort method such as

    java.util.Collections.sort(List list, Comparator c);

    ie

    class MyStringSorter implements Comparator {

    public int compare(Object o1, Object o2) {
    // your comparison code
    }
    public boolean equals(Object obj) {
    // your comparison code
    }

    }

    --
    Mike W
    VisionSet, Jun 18, 2004
    #3
  4. sks wrote:

    > Anyone got any advice on how to get a list of String's to sort 'naturally'.
    > What I mean is, take this 'sorted' list of 'hard drive capacities':
    >
    > 120GB
    > 160GB
    > 200GB
    > 20GB
    > 250GB
    > 40GB
    > 80GB
    >
    > Obviously to a human, it would be sorted as such


    Obvious only if you interpret it as *numbers* (not strings) with
    some irrelevant suffix. Make the computer interpret it like that
    as well and you're there.
    Michael Borgwardt, Jun 18, 2004
    #4
  5. sks

    Eric Sosman Guest

    sks wrote:
    > Anyone got any advice on how to get a list of String's to sort 'naturally'.
    > What I mean is, take this 'sorted' list of 'hard drive capacities':
    >
    > 120GB
    > 160GB
    > 200GB
    > 20GB
    > 250GB
    > 40GB
    > 80GB
    >
    > Obviously to a human, it would be sorted as such
    >
    > 20GB
    > 40GB
    > 80GB
    > 120GB
    > 160GB
    > 200GB
    > 250GB


    http://sourcefrog.net/projects/natsort/

    Haven't used it myself, but the site has links to
    implementations in three or four languages, including
    Java.

    --
    Eric Sosman, Jun 18, 2004
    #5
  6. sks

    Murray Guest

    Murray, Jun 18, 2004
    #6
  7. sks

    Roedy Green Guest

    On Fri, 18 Jun 2004 13:49:53 +0000 (UTC), "sks"
    <11.co.uk> wrote or quoted :

    >20GB
    >40GB
    >80GB
    >120GB
    >160GB
    >200GB
    >250GB


    Two techniques:

    1. extract the numbers as ints first and put them as fields in your
    records. Then sort on that.

    2. slower, but requires no extra field. Scan the fields to see if
    they can be converted to numbers. If so, use a numeric sort. Your
    compare method extracts the number for each compare.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 18, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul
    Replies:
    1
    Views:
    13,181
    Rogan Dawes
    Sep 14, 2004
  2. C. Barnes

    Code snippet: Natural string sorting

    C. Barnes, Jun 9, 2004, in forum: Python
    Replies:
    1
    Views:
    497
    Matteo Dell'Amico
    Jun 9, 2004
  3. Derek Basch
    Replies:
    3
    Views:
    313
    Derek Basch
    Jun 17, 2004
  4. Terry Reedy
    Replies:
    3
    Views:
    358
    Andrea Griffini
    Jun 16, 2004
  5. Connelly Barnes

    Natural string sorting

    Connelly Barnes, Jun 18, 2004, in forum: Python
    Replies:
    1
    Views:
    326
    Peter Hansen
    Jun 19, 2004
Loading...

Share This Page