Natural sorting order for alphanumeric fields

Discussion in 'Java' started by Paul, Sep 14, 2004.

  1. Paul

    Paul Guest

    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
    1.3
    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 tried two implemetations of the comparator class
    that I found on the web
    http://pierre-luc.paour.9online.fr/NaturalOrderComparator.java and
    http://www.davekoelle.com/alphanum.jsp. These seemed to work well
    EXCEPT for the cases containing decimal points
    i.e. in the following order
    1.2
    1.3
    1.25

    Does anyone know of a sort that gets around this problem.
     
    Paul, Sep 14, 2004
    #1
    1. Advertising

  2. Paul

    Rogan Dawes Guest

    Paul wrote:

    > 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
    > 1.3
    > 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 tried two implemetations of the comparator class
    > that I found on the web
    > http://pierre-luc.paour.9online.fr/NaturalOrderComparator.java and
    > http://www.davekoelle.com/alphanum.jsp. These seemed to work well
    > EXCEPT for the cases containing decimal points
    > i.e. in the following order
    > 1.2
    > 1.3
    > 1.25
    >
    > Does anyone know of a sort that gets around this problem.


    How about something like:

    public class Sorter implements Comparator {

    int compare(Object o1, Object o2) {

    if (o1 instanceof Number && o2 instanceof Number) {
    return ((Number)o1).compareTo(o2);
    } else {
    return o1.toString().compareTo(o2.toString());
    }

    }

    }

    This assumes that your objects are the right type. If they are not, you
    need to determine whether they are numbers or not, before comparing them.

    Rogan
    --
    Rogan Dawes

    *ALL* messages to will be dropped, and added
    to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
     
    Rogan Dawes, Sep 14, 2004
    #2
    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

    Natural sort order

    Paul, Sep 14, 2004, in forum: Java
    Replies:
    1
    Views:
    608
    Stefan Schulz
    Sep 14, 2004
  2. gk

    what is natural order ?

    gk, Nov 3, 2006, in forum: Java
    Replies:
    8
    Views:
    1,098
    Eric Sosman
    Nov 3, 2006
  3. Alan Davies

    Natural order sort

    Alan Davies, Sep 11, 2003, in forum: Ruby
    Replies:
    2
    Views:
    118
    Alan Davies
    Sep 15, 2003
  4. Bob

    Sorting alphanumeric

    Bob, Dec 8, 2004, in forum: Javascript
    Replies:
    12
    Views:
    205
  5. java_jazzy

    alphanumeric sorting

    java_jazzy, May 29, 2006, in forum: Javascript
    Replies:
    2
    Views:
    104
    Thomas 'PointedEars' Lahn
    May 29, 2006
Loading...

Share This Page