题解 | #成绩排序#

成绩排序

http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int sNum = in.nextInt();
        int type = in.nextInt();
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < sNum; i++) {
            list.add(new Student(in.next(), in.nextInt(), i));
        }
        if (type == 0){
            // 分数高到低
            list.stream()
                    .sorted(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getOrder))
                    .forEach(student -> System.out.println(student.getName() + " " + student.score));
        }else {
            list.stream()
                    .sorted(Comparator.comparing(Student::getScore).thenComparing(Student::getOrder))
                    .forEach(student -> System.out.println(student.getName() + " " + student.score));
        }
    }
}

class Student {
    String name;
    int score;
    int order;

    public Student(String name, int score, int order) {
        this.name = name;
        this.score = score;
        this.order = order;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
}
全部评论

相关推荐

字节一直是我的白月光,考虑到转正还是拒了日常实习。
从今天开始狠狠卷JV...:为什么你释放的offer没流到我头上
点赞 评论 收藏
分享
星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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