1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| int[][] nums = {{1,3,2}, {2,1,3}, {3,2,1}}; Arrays.sort(nums, new Comparator<int[]>() { public int compare(int[] arr1, int[] arr2) { return arr1[1] - arr2[1]; } });
int[] order = {1, 0, 2}; int[][] nums = {{2,3,1}, {1,3,2}, {2,1,3}, {2,2,3}, {2,1,2}}; Arrays.sort(nums, new Comparator<int[]>() { public int compare(int[] arr1, int[] arr2) { for (int i = 0; i < order.length; ++i) { int k = order[i]; if (arr1[k] == arr2[k]) continue; return arr1[k] - arr2[k]; } return 0; } });
int[] order = {1, 0}; int[][] nums = {{2,3,1}, {1,3,2}, {2,1,3}, {2,2,3}, {2,1,2}}; Arrays.sort(nums, new Comparator<int[]>() { public int compare(int[] arr1, int[] arr2) { for (int i = 0; i < order.length; ++i) { int k = order[i]; if (arr1[k] == arr2[k]) continue; if (k == 1) { return arr2[k] - arr1[k]; } else { return arr1[k] - arr2[k]; } } return 0; } });
for (int i = 0; i < nums.length; ++i) { for (int j = 0; j < nums[0].length; ++j) { System.out.print(nums[i][j]); System.out.print(" "); } System.out.println(); }
|