题解 | #成绩排序#

成绩排序

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();
    }

    /**
     * HJ68 成绩排序
     */
    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.get("socre"))).collect(Collectors.toList());
        }
        rst.forEach(map -> {
            System.out.println(map.get("name") + " " + map.get("socre"));
        });
    }

}

全部评论

相关推荐

07-03 16:13
嘉应学院 Python
xiaolihuam...:很明显骗子,如果是hr直接约你面试了,哪用得着内推,如果是员工的话,你得多优秀,一线员工直接加你微信,
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务