Here you can find the source of toSortedArray(Collection
public static <T extends Comparable<?>> Object[] toSortedArray(Collection<T> collection)
//package com.java2s; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; public class Main { public static <T extends Comparable<?>> Object[] toSortedArray(Collection<T> collection) { Object[] result = collection.toArray(); Arrays.sort(result);/* w ww .jav a 2 s . c o m*/ return result; } @SuppressWarnings("unchecked") public static <T> Object[] toSortedArray(Collection<T> collection, Comparator<T> cmp) { Object[] result = collection.toArray(); Arrays.sort(result, (Comparator<Object>) cmp); return result; } }