Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to sort a Set  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to sort a Set how can I do that.

Thanks.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer, is "you can't". A Set is not an ordered Collection.

A longer, and hopefully more useful, answer is that there are a variety of ways that you can get a sorted version of the information that is in a Set.

You could do toArray() on the Set, then Arrays.sort().

You could switch from a Set to a SortedSet such as TreeSet.

You could switch from a Set to a List like ArrayList and use Collections.sort().

There are a variety of reasons for choosing different options. For example, if you need the no-duplicates feature of a Set, then ArrayList is no good.
[ November 01, 2007: Message edited by: Peter Chase ]
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!