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

相关推荐

哥_留个offer先:跟他说,你这个最好用c#,微软就用c#Java不适合这个项目
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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