Skip to content

Instantly share code, notes, and snippets.

@sim642
Last active May 15, 2016 16:12
Show Gist options
  • Save sim642/82578b03a7c3b34753d0c393b82aa0d9 to your computer and use it in GitHub Desktop.
Save sim642/82578b03a7c3b34753d0c393b82aa0d9 to your computer and use it in GitHub Desktop.
Lexicographical array comparison
public final class ArrayUtils {
private ArrayUtils() {
}
public static <T extends Comparable<T>> int compare(T[] arr1, T[] arr2) {
int length = Math.min(arr1.length, arr2.length);
for (int i = 0; i < length; i++) {
int compare = arr1[i].compareTo(arr2[i]);
if (compare != 0)
return compare;
}
return arr1.length - arr2.length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment