Created
November 16, 2016 00:24
-
-
Save electrum/5a3c6296b8eec02caeb0f361d6ec3ecd to your computer and use it in GitHub Desktop.
Java sorted()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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