Java tutorial
//package com.java2s; import com.google.common.primitives.Ints; import java.util.*; public class Main { public static <T> Comparator<Collection<? extends T>> listComparator( 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(Collection<? extends T> list1, 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(Comparator<? super T> elementComparator, Collection<? extends T> list1, 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; } }