Java tutorial
//package com.java2s; import com.google.common.primitives.Ints; import javax.annotation.Nonnull; import java.util.*; public class Main { @Nonnull public static <T> Comparator<Collection<? extends T>> listComparator( @Nonnull final Comparator<? super T> elementComparator) { return new Comparator<Collection<? extends T>>() { @Override public int compare(Collection<? extends T> list1, Collection<? extends T> list2) { return compareAsList(elementComparator, list1, list2); } }; } public static <T extends Comparable<? super T>> int compareAsList(@Nonnull Collection<? extends T> list1, @Nonnull Collection<? extends T> list2) { int res = Ints.compare(list1.size(), list2.size()); if (res != 0) return res; Iterator<? extends T> elements2 = list2.iterator(); for (T element1 : list1) { res = element1.compareTo(elements2.next()); if (res != 0) return res; } return 0; } public static <T> int compareAsList(@Nonnull Comparator<? super T> elementComparator, @Nonnull Collection<? extends T> list1, @Nonnull Collection<? extends T> list2) { int res = Ints.compare(list1.size(), list2.size()); if (res != 0) return res; Iterator<? extends T> elements2 = list2.iterator(); for (T element1 : list1) { res = elementComparator.compare(element1, elements2.next()); if (res != 0) return res; } return 0; } }