Sorting String[] And Tracking Old Order

H

Hal Vaughan

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
 
T

Tom Hawtin

Hal said:
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
 
H

Hal Vaughan

Tom said:
Hal said:
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
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top