2 Replies Latest reply: Jul 5, 2007 11:08 AM by 807600 RSS

    Sort a  Generic List.

    807600
      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?)