Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Sorting String[] And Tracking Old Order

Thread view: 
Hal Vaughan - 19 Jun 2007 08:30 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.