You need to handle case where comparing second row will return 0 (equal values) and in that case return result of comparing values from first columns.
So your code can be chanced do something like:
Arrays.sort(myArr, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
int result = Integer.compare(o1[2], o2[2]);
if (result != 0)
return result;
else
return Integer.compare(o1[1], o2[1]);
}
});
But if it is possible that you will want to compare more than one column and if you want to easily create different orders you should create separate comparator for each column.
In Java 8 you can rewrite comparator like
Comparator<int[]> secondColumnt = new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[2], o2[2]);
}
}
by writing it as
Comparator<int[]> secondColumnt = Comparator.comparingInt(row -> row[2]);
so lets also create comparator for first column
Comparator<int[]> firstColumnt = Comparator.comparingInt(row -> row[1]);
Now since Java 8 we can create comparators from already existing comparators using thenComparing method.
So if we want to create comparator which will first compare values in second columns and if they are equal comparator for first first column we can write it as
Comparator<int[]> secondThenFirstColumn = secondColumnt.thenComparing(firstColumnt);
So your final code can look like
Arrays.sort(myArr, secondThenFirstColumn);
or
Arrays.sort(myArr, secondColumnt.thenComparing(firstColumnt));
or if you don't want to create separate predefined comparators
Arrays.sort(myArr, Comparator.<int[]>comparingInt(row -> row[2]).thenComparingInt(row -> row[2]));