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
Tom Hawtin - 19 Jun 2007 08:56 GMT
> 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
Hal Vaughan - 20 Jun 2007 00:36 GMT
>> 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
[quoted text clipped - 6 lines]
> 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
Roedy Green - 29 Jun 2007 16:21 GMT
On Tue, 19 Jun 2007 03:30:42 -0400, Hal Vaughan
<hal@thresholddigital.com> 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