1、排序
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/*
* 下面代码实现了间接排序
*
*/
public class ArraySort {
static Integer []r = new Integer[105];
static Integer []w = new Integer[105];
static Scanner in = new Scanner(System.in);
static int [][]a = new int[5][];
public static void main(String[] args) {
int [][]a = {{3,1},{5,2},{1,5}};
twoDimSort(a);
}
/*
* 2维度数组按照列排序
*/
static void twoDimSort(int [][]a) {
Arrays.sort(a,new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[0]-o1[0]; //根据二维数组第一列按照降序排列
}
});
for(int i = 0; i < a.length; ++i) {
for(int j = 0; j < a[i].length; ++j) {
System.out.printf("%-3d",a[i][j]);
}
System.out.println();
}
}
/*
* 利用根据权重w数组排序 r数组,r里面记录的是相对位置的序号
*/
static void inDirectSirt() {
System.out.println(a.length);
int n = in.nextInt();
for(int i = 0; i < n; ++i) {
w[i] = in.nextInt();
}
for(int i = 0; i < n; ++i) r[i] = i;
Arrays.sort(r,0,n,cmp);
for(int i = 0; i < n; ++i) {
System.out.printf("%-3d",r[i]);
}
}
static Comparator<Integer> cmp = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return w[o2]-w[o1];
}
};
}