I need to sort such an array by descending the elements of the first column. In the case of equality of elements in the first column, the elements of the second column must be sorted in ascending order. But this what I need else is to check and put empty row to the end of matrix, and row with only one element put prior to the same which has more elements.
For example in this array {3}
- it is the first row, {}
- the last one.
int[][] arr = {{1, 2, 3}, {}, {3}, {1, 4}, {3, 2}, {3, 3, 5}};
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] == o2[0])
return o1[1] - o2[1];
return o2[0] - o1[0];
}
});
for (int[] ints : arr) {
for (int anInt : ints) {
System.out.print(anInt + " ");
}
System.out.println();
}