Jeremy Stein - Brain

« »

Java – Sort a set

Here’s my utility to method to sort a set in Java 5:

@SuppressWarnings("unchecked")
public static <T extends Comparable> List<T> asSortedList(Collection<T> collection) {
  T[] array = collection.toArray(
    (T[])new Comparable[collection.size()]);
  Arrays.sort(array);
  return Arrays.asList(array);
}

December 18, 2006 1 Comment.

One Comment

  1. Keith Corlett replied:

    Thank you Jeremy! I wasted a couple of hours trying to find an implementation in the core API… Yet another curious omission (IMHO).

    Thanking you sir, Keith.

    April 11th, 2009 at 12:32 am. Permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?

« »