题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<String, Integer> map = new HashMap<>(); int n = in.nextInt(); int flag = in.nextInt(); int finalFlag = flag == 0 ? -1 : 1; while (n > 0) { map.put((10000 - n) + in.next(), in.nextInt()); n--; } List<Map.Entry<String, Integer>> list = new ArrayList<>(); list.addAll(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { if (o1.getValue().equals(o2.getValue())) { return o1.getKey().compareTo(o2.getKey()); } return (o1.getValue() - o2.getValue()) * finalFlag; } }); for (Map.Entry<String, Integer> entry : list) { System.out.println(entry.getKey().substring(4) + " " + entry.getValue()); } } }