728x90
결론적으로는 불가능함
Map의 정렬은 Key값을 기준으로 정렬이 가능하고
Value들을 활용하여 정렬하려면 따로 빼줘서 정렬해줘야 한다..
참고 : https://codechacha.com/ko/java-sort-map/
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
48
49
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static class Point{
int idx;
int total;
public Point(int idx, int total) {
this.idx = idx;
this.total = total;
}
}
public static void main(String[] args) throws IOException{
Map<String, Point> map = new HashMap<>();
map.put("Nepal", new Point(1,100));
map.put("United States", new Point(2,400));
map.put("India", new Point(3,50));
map.put("England", new Point(4,1000));
map.put("Australia", new Point(5,300));
List<Point> valueList = new ArrayList<>(map.values());
valueList.sort((p1,p2)-> p2.total-p1.total);
for (Point point : valueList) {
System.out.println(point.total+" "+point.idx);
}
bw.write("");
bw.flush();
bw.close();
}
// private static int stoi(String input) {
// return Integer.parseInt(input);
// }
}
|
cs |
output:
1000 4
400 2
300 5
100 1
50 3