PublicMetacell[][] Getbycolumn (FinalintColumnIndex,intDecisionindex) {//"Attention," final .
metacell[][] Array =NewMETACELL[M][2];//Entry<metacell, metacell> reference http://blog.csdn.net/sunmenggmail/article/details/8952712 andhttp://www.cnblogs.com/fstang/archive/2013/04/20/3032097.html
for(inti=0; i<m; i++) {
Array[i][0]=a[i][columnindex];
Array[i][1]=a[i][decisionindex];
}
Arrays.sort (Array,NewComparator<metacell[]> () {//a two-dimensional array is sorted by a column, and you can also use the map
PublicintCompare (metacell[] O1, metacell[] O2) {//any multidimensional array can be viewed as a one-dimensional array, and each element in a one-dimensional array is a one-dimensional array
returnO1[columnindex].compareto (O2[columnindex]);//comparison: Greater than 0 means ascending
}
} );
returnArray
}
The above default ascending order. You can modify the comparator interface.
The following is sorted by multiple columns "in the 1th column, followed by the 2nd--When the first column appears the same value, sorted by column 2nd"
Import java.util.Arrays;
ImportJava.util.Comparator;
PublicclassArraySort {
PublicStatic voidSortint[] ob,Finalint[] order) {
Arrays.sort (OB,NewComparator<object> () {
PublicintCompare (object O1, Object O2) {
int[] one = (int[]) O1;
int[] both = (int[]) O2;
for(inti = 0; i < order.length; i++) {
intk = Order[i];
if(One[k] > Two[k]) {
return1;
}Elseif(One[k] < two[k]) {
return-1;
}Else{
Continue;//If the result is equal by one condition, the second condition is used to compare.
}
}
return0;
}
});
}
PublicStaticvoidMain (string[] args) {
intArray[][] =Newint[][] {
{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,Newint[] {0,1});//Compare to the first column, and then compare the second column if the same
for(inti = 0; i < Array.Length; i++) {
for(intj = 0; J < Array[i].length; J + +) {
System.out.print (Array[i][j]);
System.out.print ("\ t");
}
System.out.println ();
}
}
}
Java multidimensional arrays sorted by a column