Java tutorial
//package com.java2s; import javax.annotation.Nonnull; import java.util.*; public class Main { public static <T> int compareAsIterable(@Nonnull Comparator<? super T> comparator, @Nonnull Iterable<? extends T> it1, @Nonnull Iterable<? extends T> it2) { Iterator<? extends T> elements2 = it2.iterator(); for (T element1 : it1) { T element2; try { element2 = elements2.next(); } catch (NoSuchElementException ex) { return 1; } int res = comparator.compare(element1, element2); if (res != 0) return res; } if (elements2.hasNext()) { return -1; } return 0; } public static <T extends Comparable<? super T>> int compareAsIterable(@Nonnull Iterable<? extends T> it1, @Nonnull Iterable<? extends T> it2) { Iterator<? extends T> elements2 = it2.iterator(); for (T element1 : it1) { T element2; try { element2 = elements2.next(); } catch (NoSuchElementException ex) { return 1; } int res = element1.compareTo(element2); if (res != 0) return res; } if (elements2.hasNext()) { return -1; } return 0; } }