public MetaCell[][] getByColumn(final int columnIndex, int decisionIndex) {//【注意】final咯
MetaCell[][] array = new MetaCell[m][2];//Entry<MetaCell, MetaCell>参考http://blog.csdn.net/sunmenggmail/article/details/8952712 和 http://www.cnblogs.com/fstang/archive/2013/04/20/3032097.html
for(int i=0; i<m; i++){
array[i][0]=A[i][columnIndex];
array[i][1]=A[i][decisionIndex];
}
Arrays.sort(array, new Comparator<MetaCell[]>(){//二维数组按照某列进行排序,你也可以采用Map
public int compare(MetaCell[] o1, MetaCell[] o2) {//任何多维数组可看成一个一维数组,一维数组中每个元素是一个一维数组
return o1[columnIndex].compareTo(o2[columnIndex]);//比较:大于0则表示升序
}
} );
return array;
}
MetaCell[][] array = new MetaCell[m][2];//Entry<MetaCell, MetaCell>参考http://blog.csdn.net/sunmenggmail/article/details/8952712 和 http://www.cnblogs.com/fstang/archive/2013/04/20/3032097.html
for(int i=0; i<m; i++){
array[i][0]=A[i][columnIndex];
array[i][1]=A[i][decisionIndex];
}
Arrays.sort(array, new Comparator<MetaCell[]>(){//二维数组按照某列进行排序,你也可以采用Map
public int compare(MetaCell[] o1, MetaCell[] o2) {//任何多维数组可看成一个一维数组,一维数组中每个元素是一个一维数组
return o1[columnIndex].compareTo(o2[columnIndex]);//比较:大于0则表示升序
}
} );
return array;
}
以上默认升序。可修改Comparator接口即可。
以下是按多列排序【以第1列为准,第2列次之——当第一列出现相同值,用第2列排序】
import java.util.Arrays;
import java.util.Comparator;
public class ArraySort {
public static void sort(int[][] ob, final int[] order) {
Arrays.sort(ob, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
int[] one = (int[]) o1;
int[] two = (int[]) o2;
for (int i = 0; i < order.length; i++) {
int k = order[i];
if (one[k] > two[k]) {
return 1;
} else if (one[k] < two[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 } };
sort(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(" ");
}
System.out.println();
}
}
}
public class ArraySort {
public static void sort(int[][] ob, final int[] order) {
Arrays.sort(ob, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
int[] one = (int[]) o1;
int[] two = (int[]) o2;
for (int i = 0; i < order.length; i++) {
int k = order[i];
if (one[k] > two[k]) {
return 1;
} else if (one[k] < two[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 } };
sort(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(" ");
}
System.out.println();
}
}
}