import java.util.*; public static void sortIntArray(int[][] arObjects, final int[] arOrders) { Arrays.sort(arObjects, new Comparator<Object>() { public int compare(Object oObjectA, Object oObjectB) { int[] arTempOne = (int[])oObjectA; int[] arTempTwo = (int[])oObjectB; for (int i = 0; i < arOrders.length; i++) { int k = arOrders[i]; if (arTempOne[k] > arTempTwo[k]) { return 1; } else if (arTempOne[k] < arTempTwo[k]) { return -1; } else { continue; } } return 0; } }); }
調用方法是:
public static void main(String[] args) { int array[][] = new int[][] { { 12, 34, 68, 32, 9, 12, 545 }, { 34, 72, 82, 57, 56, 0, 213 }, { 12, 34, 68, 32, 21, 945, 23 }, { 91, 10, 3, 2354, 73, 34, 18 }, { 12, 83, 189, 26, 27, 98, 33 }, { 47, 23, 889, 24, 899, 23, 657 }, { 12, 34, 68, 343, 878, 235, 768 }, { 12, 34, 98, 56, 78, 12, 546 }, { 26, 78, 2365, 78, 34, 256, 873 } }; sortIntArray(int[][], int[])(array, new int[] { 0, 1 }); // 先根據第一列比較,若相同則再比較第二列 for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]); System.out.print("\t"); } System.out.println(); } }
註:本文轉載自:http://blog.sina.com.cn/s/blog_5a15b7d101014ngm.html