Skip to content

Instantly share code, notes, and snippets.

@electrum
Created November 16, 2016 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electrum/5a3c6296b8eec02caeb0f361d6ec3ecd to your computer and use it in GitHub Desktop.
Save electrum/5a3c6296b8eec02caeb0f361d6ec3ecd to your computer and use it in GitHub Desktop.
Java sorted()
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import static java.util.Comparator.naturalOrder;
public final class Collections
{
private Collections() {}
public static <T extends Comparable<T>> List<T> sorted(Collection<T> collection)
{
return sorted(collection, naturalOrder());
}
public static <T> List<T> sorted(Collection<T> collection, Comparator<T> comparator)
{
List<T> list = new ArrayList<>(collection);
list.sort(comparator);
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment