From 3eb73c4ba53dc273e9724d1b4a498502628eaf8f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 14 Feb 2016 12:52:21 +0000 Subject: Extract ordering functions to MammothLists --- src/main/java/org/zwobble/mammoth/util/MammothLists.java | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/main/java/org/zwobble/mammoth/util') 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 List eagerFlatMap(Iterable iterable, Function> function) { return ImmutableList.copyOf(Iterables.concat(Iterables.transform(iterable, function))); } + + public static > List orderedBy(Iterable iterable, Function getKey) { + return orderBy(getKey).sortedCopy(iterable); + } + + private static > Ordering orderBy(Function getKey) { + return Ordering.from((first, second) -> getKey.apply(first).compareTo(getKey.apply(second))); + } } -- cgit v1.2.3