JAVA 必须要用到Collections.sort中的自定义排序方法

成绩排序

http://www.nowcoder.com/questionTerminal/0383714a1bb749499050d2e0610418b1

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int numPeople = sc.nextInt();
            int option = sc.nextInt();

            List<Student> stuList=new ArrayList<>();
            for(int i=0;i<numPeople;i++){
                stuList.add(new Student(sc.next(),sc.nextInt()));
            }
            //降序
            if(option==0){
                Collections.sort(stuList, new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.score-o1.score;
                    }
                });
            }else if(option==1){
                Collections.sort(stuList, new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.score-o2.score;
                    }
                });
            }

            for(int i=0;i<stuList.size();i++){
                System.out.println(stuList.get(i).name+" "+stuList.get(i).score);
            }

        }
    }
}

class Student{
    public String name;
    public int score;
    public Student(String name,int score){
        this.name=name;
        this.score=score;
    }
}
全部评论

相关推荐

字节一直是我的白月光,考虑到转正还是拒了日常实习。
从明天开始狠狠卷JV...:为什么你释放的offer没流到我头上
点赞 评论 收藏
分享
05-26 22:25
门头沟学院 Java
Java小肖:不会是想叫你过去把你打一顿吧,哈哈哈
点赞 评论 收藏
分享
把实习生当正职使昨天第一天就加班,晚上连口饭都没吃上,以后日子咋过,我不想干了
码农索隆:实习不怕忙,就怕干的活重复且没难度,要干就干那种有深度有难度的任务,这样才能快速的提升
实习吐槽大会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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