views:

329

answers:

6

Hi All

I have a generic Collection and am trying to work out how I can sort the items contained within it. Ive tried a few things but I cant get any of them working.

A: 
HonorGod
Thx for your reply but i have a generic Collection this example not work for me
Mercer
+1  A: 

You can't if T is all you get. You have to have the it injected by the provider via:

Collection<T extends Comparable>

or pass in the Comparator via the Collections.sort(...) method

arcticpenguin
+1  A: 

A Collection does not have an ordering, so wanting to sort it does not make sense. You can sort List instances and arrays, and the methods to do that are Collections.sort() and Arrays.sort()

Michael Borgwardt
+2  A: 

Collections by themselves do not have a predefined order, therefore you must convert them to a java.util.List. Then you can use one form of java.util.Collections.sort

Collection< T > collection = ...;

List< T > list = new ArrayList< T >( collection );

Collections.sort( list );
 // or
Collections.sort( list, new Comparator< T >( ){...} );

// list now is sorted
Alexander Pogrebnyak
Although this solves the problem, this will not be the fastest way to do this, unless the collection itself is already a List.
Fortega
@Fortega: Then enlighten me what IS the fastest way to sort a general Collection while retaining all of its elements. BTW, this is exactly the method used by Google Collections `Ordering.sortedCopy`.
Alexander Pogrebnyak
It depends on the type of the collection. For collections of which the toArray() method (called in constructor of ArrayList) needs iteration over all the elements (for example: a Set), sorting might need an extra loop over all elements. You could use google collections TreeMultiset, which allows duplicates. Although the differences will probably be not very big and in the same order of magnitude.
Fortega
A: 

You have two basic options provided by java.util.Collections:

Depending on what the Collection is, you can also look at SortedSet or SortedMap.

polygenelubricants
+1  A: 

If your collections object is a list, I would use the sort method, as proposed in the other answers.

However, if it is not a list, and you don't really care about what type of Collection object is returned, I think it is faster to create a TreeSet instead of a List:

TreeSet sortedSet = new TreeSet(myComparator);
sortedSet.addAll(myCollectionToBeSorted);
Fortega
TreeSet requires Comparable to be implemented, if you want the set sorted. This is what I usually do when I need a sorted Collection. TreeSet will also throw away duplicates.
David
TreeSet does not require Comparable to be implemented if you use a Comparator. However, you are right about the duplicates.
Fortega
OOPS- missed the comparator sitting in there. I always implement Comparable, so I missed it completely.
David
Note that a TreeSet is feasible only when the collection does not contain duplicates.Also, populating an ArrayList and then sorting it is faster than populating a TreeSet. Though both approaches are O(N log N), a TreeSet has a higher constant factor because of the red-black tree manipulations and the larger number of memory allocations.Still, I sometimes use a TreeSet to make my code more concise, even though that's slower than using an ArrayList.
Jared Levy