Last active
May 15, 2016 16:12
-
-
Save sim642/82578b03a7c3b34753d0c393b82aa0d9 to your computer and use it in GitHub Desktop.
Lexicographical array comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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