-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocreSort.java
More file actions
47 lines (43 loc) · 1.79 KB
/
Copy pathSocreSort.java
File metadata and controls
47 lines (43 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package AlgorithmTest;
import java.util.*;
/*
*题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
* */
public class SocreSort {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int num = Integer.parseInt(reader.nextLine()); //拿到要输入的行数
int Selected = Integer.parseInt(reader.nextLine()); //升序还是降序
Map<String, Integer> Students = new HashMap<>();
for (int i = 0; i < num; i++) {
String a = reader.nextLine();
String[] result = a.split(" ");
Students.put(result[0], Integer.parseInt(result[1]));
}
// Map==>List
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(Students.entrySet());
switch (Selected) {
case 1: //正序
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue() - o2.getValue();
}
});
break;
case 0: //反序
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue() - o1.getValue();
}
});
break;
default:
break;
}
for (Map.Entry<String, Integer> entry : list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}