题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*;
import java.util.stream.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
sortStudent();
}
private static void sortStudent() {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int sortType = in.nextInt();
List<Map<String, Object>> rst = new ArrayList<>();
while (num > 0 && in.hasNext()) {
Map<String, Object> map = new HashMap<>();
String name = in.next();
int socre = in.nextInt();
map.put("name", name);
map.put("socre", socre);
rst.add(map);
num--;
}
if (sortType == 0) {
rst = rst.stream().sorted(Comparator.comparingInt(map -> (int) ((
Map<String, Object>) map).get("socre")).reversed()).collect(
Collectors.toList());
} else if (sortType == 1) {
rst = rst.stream().sorted(Comparator.comparingInt(map -> (int) ((
Map<String, Object>) map).get("socre"))).collect(
Collectors.toList());
}
rst.forEach(map -> {
System.out.println(map.get("name") + " " + map.get("socre"));
});
}
}
查看11道真题和解析
