Sorting String[] And Tracking Old Order

Discussion in 'Java' started by Hal Vaughan, Jun 19, 2007.

  1. Hal Vaughan

    Hal Vaughan Guest

    I can do this with my own sort, but before I do that, I'd like to know if
    there is a built in way to do this or a standard method.

    I will be taking a set of values for a String[] variable from a data table.
    I need to keep the association of each member of the String[] in place so I
    can relocate it's row in my table, but I'd like to sort them for display on
    a JComboBox or JList. If I use Arrays.sort(myStringArray), it will sort
    them, but then I lose any way of tracking the original order.

    I know I can write my own sort routine and sort the String[] myself and keep
    track of the original values, but is there a quicker and/or easier way?

    Thanks!

    Hal
     
    Hal Vaughan, Jun 19, 2007
    #1
    1. Advertising

  2. Hal Vaughan

    Tom Hawtin Guest

    Hal Vaughan wrote:
    >
    > I will be taking a set of values for a String[] variable from a data table.
    > I need to keep the association of each member of the String[] in place so I
    > can relocate it's row in my table, but I'd like to sort them for display on
    > a JComboBox or JList. If I use Arrays.sort(myStringArray), it will sort
    > them, but then I lose any way of tracking the original order.


    I suggest introducing a class that references a String and the
    appropriate row ID. Then write a Comparator that orders the instances of
    the introduced class on the basis of their Strings.

    Note, the "natural order" of String is probably not want you want.
    java.text.Collator/RuleBasedCollator provide a more natural way to sort
    Strings.

    Tom Hawtin
     
    Tom Hawtin, Jun 19, 2007
    #2
    1. Advertising

  3. Hal Vaughan

    Hal Vaughan Guest

    Tom Hawtin wrote:

    > Hal Vaughan wrote:
    >>
    >> I will be taking a set of values for a String[] variable from a data
    >> table. I need to keep the association of each member of the String[] in
    >> place so I can relocate it's row in my table, but I'd like to sort them
    >> for display on
    >> a JComboBox or JList. If I use Arrays.sort(myStringArray), it will sort
    >> them, but then I lose any way of tracking the original order.

    >
    > I suggest introducing a class that references a String and the
    > appropriate row ID. Then write a Comparator that orders the instances of
    > the introduced class on the basis of their Strings.


    That's what I was thinking about doing, but I've learned that the Java API
    always has some more surprises, so I was hoping there might be something
    available that took care of that in some automatic way. I don't mind doing
    it, but wanted to be sure there wasn't an easier way first. I was using my
    own sort routine for several months before I learned about Arrays.sort()!
    (Being self taught with no guide to help you can be frustrating at times!)

    > Note, the "natural order" of String is probably not want you want.
    > java.text.Collator/RuleBasedCollator provide a more natural way to sort
    > Strings.


    I just read up on that. I guess I'd say that fits in to the category of API
    surprises I didn't know about. Thanks!

    Hal
     
    Hal Vaughan, Jun 20, 2007
    #3
  4. Hal Vaughan

    Roedy Green Guest

    On Tue, 19 Jun 2007 03:30:42 -0400, Hal Vaughan
    <> wrote, quoted or indirectly quoted someone
    who said :

    > but then I lose any way of tracking the original order.


    Add an int field to your objects. Then when you have them in original
    order do this
    int ordinal = 0;
    for ( thing : things)
    {
    thing.order = ordinal++;
    }

    now you can sort them back the way they were by sorting on "order".

    Another method is to simply create an ArrayList of the objects in the
    original order and sort a copy. It is perfectly legit to have two
    ArrayLists sharing the same set of objects.

    Another method is to thread the objects together with Thing next
    field. Then you can chase the objects in original order no matter what
    order the list is.

    The second method is easiest and has very little extra overhead.

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 29, 2007
    #4
    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. Tom St Denis

    Sorting a string aray in order of string length

    Tom St Denis, Jul 9, 2003, in forum: C Programming
    Replies:
    9
    Views:
    480
    Kevin Easton
    Jul 10, 2003
  2. Jim Hill
    Replies:
    3
    Views:
    403
    Jim Hill
    Feb 12, 2007
  3. Tio
    Replies:
    2
    Views:
    405
    Victor Bazarov
    Jun 14, 2006
  4. *Prot3anThr3ad*

    old repository for old C++ source code

    *Prot3anThr3ad*, Sep 29, 2006, in forum: C++
    Replies:
    6
    Views:
    381
    *Prot3anThr3ad*
    Oct 2, 2006
  5. John Henry
    Replies:
    24
    Views:
    1,016
    alex23
    May 30, 2008
Loading...

Share This Page