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 / First Aid / August 2006

Tip: Looking for answers? Try searching our database.

Vector Sort

Thread view: 
Christopher Smith - 26 Aug 2006 01:45 GMT
Hi All -

What's the best way to sort a vector of Integers?

Thanks, Chris
Knute Johnson - 26 Aug 2006 02:54 GMT
> Hi All -
>
> What's the best way to sort a vector of Integers?
>
> Thanks, Chris

Chris:

The simplest way is to remove the elements from the Vector to an array.
 The using Arrays.sort(), sort them and put them back into the vector.
 You will need to write a Comparator to use with the sort.

Well I just looked at TreeSet and that might actually be simpler.  Just
pass your Vector to the TreeSet constructor and they should be sorted
when you retrieve them.  I think I like this answer better.

Signature

Knute Johnson
email s/nospam/knute/

Patricia Shanahan - 26 Aug 2006 03:03 GMT
>> Hi All -
>>
[quoted text clipped - 7 lines]
>  The using Arrays.sort(), sort them and put them back into the vector.
>  You will need to write a Comparator to use with the sort.

Why not just apply Collections.sort to the vector, which does
essentially the same thing with one call?

Also, Integer is Comparable, so a Comparator is needed if, and only if,
the sort is in some special order.

Patricia
Knute Johnson - 26 Aug 2006 03:49 GMT
>>> Hi All -
>>>
[quoted text clipped - 15 lines]
>
> Patricia

Patricia is right that is an even simple solution.  Ignore everything I
said :-).

Signature

Knute Johnson
email s/nospam/knute/

Oliver Wong - 28 Aug 2006 21:27 GMT
>> Hi All -
>>
[quoted text clipped - 11 lines]
> pass your Vector to the TreeSet constructor and they should be sorted when
> you retrieve them.  I think I like this answer better.

   Putting the items in the vector into a treeset may cause you to lose
duplicate values.

   - Oliver


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.