Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

collection to string array possible!  RSS feed

 
swetha deshmukh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we copy a collection to string array to perform sorting?If so how?Is there any other method to sort the elements in a collection
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of static sort() methods in the java.util.Collections class that can be applied to any List object. Otherwise, you can use the toArray() method which every collection has, like this:

String[] strings = (String[]) collection.toArray(new String[collection.size()]);
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use collection which implements SortedSet, SortedMap. (TreeSet, TreeMap)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!