题解 | #成绩排序#

成绩排序

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

    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<String, Object>) map).get("socre"))).collect(
                      Collectors.toList());
        }
        rst.forEach(map -> {
            System.out.println(map.get("name") + " " + map.get("socre"));
        });
    }

}

全部评论

相关推荐

刷牛客的单身狗很认真:全国可飞,支持007 上班时间,是吧?
点赞 评论 收藏
分享
Java面试先知:我也是和你一样的情况,hr 说等开奖就行了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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