With my following code I try to sort a two dimensional array
int[][] d2 = {
{4,5,1},
{4,1,1},
{1,7,1},
{3,3,2},
{1}
};
java.util.Arrays.sort(d2, new java.util.Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
I display the array after sorting
for (int r=0; r<d2.length; r++) {
for (int c=0; c<d2[r].length; c++) {
System.out.print(" " + d2[r][c]);
}
System.out.println("");
}
The Result I get is like this
1 7 1
1
3 3 2
4 5 1
4 1 1
I want the result two come like this
1
1 7 1
3 3 2
4 5 1
4 1 1
What is required to be done to get the array sorted like the above?
I tried replacing the {1}
with {1,0,0}
but it still gives the same result even for {1,0,1}
.