/*
*
map是一种开发过程中经常使用的k-v数据结构,有个map保存了书名和书字数的关系,编写代码对map里面的书按照字数进行升序排序
输入描述:
第1行为map项的个数
第2到N行为k-v,以逗号分隔,值大小为int类型(不小于0)
输出描述:
输出map按照值升序排序后的结果
示例1
输入
4
a2,100
a1,100
b,300
c,200
输出
a2,100
a1,100
c,200
b,300
*
* */
import java.util.*;
public class MapSort {
public static void main(String[] args) {
LinkedHashMap<String,Integer> map = new LinkedHashMap<>();//保证输入顺序
Scanner sc = new Scanner(System.in);
int i = 0;
int n = Integer.parseInt(sc.nextLine());//获取map个数
while (i<n){
i++;
String s = sc.nextLine();
String[] split = s.split(",");
String key = split[0];
Integer value = Integer.valueOf(split[1]);
map.put(key,value);
}
List<Map.Entry<String,Integer>> list = new ArrayList<>(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) {
int result = o1.getValue()-o2.getValue();
if(result == 0){
return 0;//相等时则不操作
}else{
return result;
}
}
});
for (int i1 = 0; i1 < list.size(); i1++) {
System.out.println(list.get(i1).getKey()+","+list.get(i1).getValue());
}
}
}