字符串数组的排序,重写comparator 方法compare()。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[][] name = new String[n][2];
for(int i=0; i < n; i++) {
name[i][0] = sc.next();
double x = sc.nextDouble(), y =sc.nextDouble();
double t = y / x;
if(t <= 0.3) name[i][1] = " 5";
else if(t <= 0.6) name[i][1] = " 4";
else name[i][1] = " 3";
}
Arrays.sort(name, new Comparator<String[]>(){
public int compare(String[] a, String[] b) {
return a[0].compareTo(b[0]);
}
});
for(int i=0; i < n; i++) {
System.out.println(name[i][0] + name[i][1]);
}
}
}
原文:https://www.cnblogs.com/lixyuan/p/13204881.html