题解 | #成绩排序#

成绩排序

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;
    }
}
全部评论

相关推荐

11-08 13:58
门头沟学院 Java
程序员小白条:竟然是蓝桥杯人才doge,还要花钱申领的offer,这么好的公司哪里去找
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务