Sort a Generic List.

I was wondering how to sort a list of generics. I am an Experienced C++ programmer and know that I can do this with STL quite easily but cannot see an equivelant in Java.

I was expecting to have something like the following code

//perhaps an equivelant of a functional object added here in the constructor

List<String> listStrings =new Vector<String>();

listStrings.add("Hello");

listStrings.add("Cruel");

listStrings.add("World");

sort(listStrings);

for(String strString : listStrings)

System.out.println(strString);

I know that I can achieve a similar result by casting the List to an array and then doing a sort and then using addAll() to make it back into a list or I could just use a SortedList of some kind. e.g.

SortedSet<String> setStrings =new TreeSet<String>();

I was just windering if there was another method that would be cleaner than this. (A Sorted List of some kind would be very clean unless you wanted to sort the array at a later stage after using it in its unsorted form first, also what if you want duplicates?)

[1342 byte] By [Crispsa] at [2007-11-27 9:47:56]
# 1
K, I found it in java.utils.CollectionsCollections.sort(listStrings);and there is another sort overload which allows you to specify a Comparator.Perfect.
Crispsa at 2007-7-13 0:01:07 > top of Java-index,Java Essentials,New To Java...
# 2
When you done enough with Java's generics, I'm interested in yourimpression of it versus C++ templates.
BigDaddyLoveHandlesa at 2007-7-13 0:01:07 > top of Java-index,Java Essentials,New To Java...