Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting TreeSet using Comparator

 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic