TreeMap排序 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
TreeMap排序Key,value的排序比较麻烦
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int num = in.nextInt();
int isAscend = in.nextInt()==1?1:-1;
SortedMap<Integer,ArrayList<String>> map = new TreeMap<>((c1,c2)->isAscend*(c1-c2));
for(int i=0;i<num;i++){
String name = in.next();
int score = in.nextInt();
if(!map.containsKey(score)) map.put(score,new ArrayList<>());
map.get(score).add(name);
}
for(int score : map.keySet()){
for(String s : map.get(score)){
System.out.println(s+" "+score);
}
}
}
}


