aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/util
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-14 12:52:21 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-14 12:52:21 +0000
commit3eb73c4ba53dc273e9724d1b4a498502628eaf8f (patch)
treee9cd203b53f8fc9e4ffaf28727571c49140da70e /src/main/java/org/zwobble/mammoth/util
parent29d3c2477d438f6c69d16188e75664fdc27655d5 (diff)
Extract ordering functions to MammothLists
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/util')
-rw-r--r--src/main/java/org/zwobble/mammoth/util/MammothLists.java9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/mammoth/util/MammothLists.java b/src/main/java/org/zwobble/mammoth/util/MammothLists.java
index 59f23f8..d6d1136 100644
--- a/src/main/java/org/zwobble/mammoth/util/MammothLists.java
+++ b/src/main/java/org/zwobble/mammoth/util/MammothLists.java
@@ -3,6 +3,7 @@ package org.zwobble.mammoth.util;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
+import com.google.common.collect.Ordering;
import java.util.List;
@@ -22,4 +23,12 @@ public class MammothLists {
public static <T, R> List<R> eagerFlatMap(Iterable<T> iterable, Function<T, Iterable<R>> function) {
return ImmutableList.copyOf(Iterables.concat(Iterables.transform(iterable, function)));
}
+
+ public static <T, R extends Comparable<R>> List<T> orderedBy(Iterable<T> iterable, Function<T, R> getKey) {
+ return orderBy(getKey).sortedCopy(iterable);
+ }
+
+ private static <T, R extends Comparable<R>> Ordering<T> orderBy(Function<T, R> getKey) {
+ return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second)));
+ }
}