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

how to easily sort the same TreeSet few times?

 
Victor Fernandez
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I am typing my own example of TreeSet.

Basically I have Objects of type Item which have a name of type String and weight of type int. :



Well, if I create a TreeSet and I start to add objects they are sorted by name as I specified in the compareTo method.



At this point my question is: How can I sort the treeSet using for example the weight?
My approach is as follows:
- I have created a comparator and I am trying to sort the TreeSet with it.



But I don't know how to sort the TreeSet without creating a new one.... I got the items sorted by weight using an array:



How can I sort the treeSet without having to create a new one?

Thanks
 
Victor Fernandez
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read in the API that TreeSet is a Red-black implementation of the NavigableSet interface. And the NavigableSet is a SortedSet extended with navigation methods.

Following with SortedSet the API says that is a Set whose elements are automatically sorted when the SortedSet is created.

Probably the answer to my previous answer could be that : Does not make sense to sort several times a TreeSet! , it is sorted using the sorting we specified in the moment of its creation. And when we add new elements it continues sorted.

Am I right?

Cheers.
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have your TreeSet<Item> ts and it's sorted based on your compareTo() method; you
now want to sort it some other way? I believe you are right, you can't do it without creating
a new data structure.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the static Collections method
public static <T> void sort(List<T> list, Comparator<? super T> c)

? Shouldn't that work? Ok, in the end it also uses an array
This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. (API)


cheers
Bob
 
Victor Fernandez
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the static Collections method

public static <T> void sort(List<T> list, Comparator<? super T> c)


? Shouldn't that work? Ok, in the end it also uses an array

This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. (API)



cheers


We can sort the treeSet using a List instead of an array. The code could be:


I am creating an ArrayList from a Collection, in this case a TreeSet.

Cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic