aspose file tools*
The moose likes Beginning Java and the fly likes Sorting TreeSet using Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting TreeSet using Comparator" Watch "Sorting TreeSet using Comparator" New topic
Author

Sorting TreeSet using Comparator

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
hello

i'll tell u what ive been doing so far..
Ive got 3 classes,
1) Person (interface extends Comparable)
2) ThePerson (implements Person)
3) PersonList (TreeSet holds collection of Person class)

the Persons will be sorted based on their ID numbers.. so this should be done automatically because im using TreeSet, is this correct?

Now, i want the collection to be slso sorted using another attribue that is "name", iv got a comparator ready to do that, but i cant use

collections.sort(treeSet, comp) <-- because its a Set!!

can some1 please please help me out
Hannah
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look at the TreeSet constructors. Can you see a way to make a new TreeSet sorted by a comparator using the original for data?
[ July 28, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
aha i c what u mean
but i dont wana construct a new 1.... i want the same 1 to be in different order, meaning, i can have the same either order by the ID or by the name... do u get me?

thanks anyway
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup, but I think you're out of luck. Constructing a new one is not that big a deal as long as you kept the variable private so nobody is holding a reference to the old one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting TreeSet using Comparator