list排序

114 阅读1分钟
public class Student {
    private String name;
    private Integer score;
    private Integer height;
    private String college;
    private String address;

    public Student(String name,Integer score,Integer height,String college,String address) {
        this.name = name;
        this.score = score;
        this.height = height;
        this.college = college;
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public Integer getScore() {
        return score;
    }

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

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

1、按照对象单属性升序,降序排序

public class Test {
    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("张三",90,180,"电气学院","北京"));
        studentList.add(new Student("李四",80,165,"计算机学院","上海"));
        studentList.add(new Student("王五",91,170,"财经学院","上海"));
        studentList.add(new Student("赵明",80,182,"计算机学院","北京"));
        studentList.add(new Student("钱海",75,181,"计算机学院","广州"));
        studentList.add(new Student("孙理",82,172,"财经学院","上海"));
        studentList.add(new Student("周伟",90,168,"电气学院","广州"));
        studentList.add(new Student("郑亮",80,178,"财经学院","广州"));
        System.out.println("原始数据:");
        studentList.forEach(s -> {
            System.out.println(s.getName()+" "+s.getScore()+" "+s.getHeight()+" "+s.getCollege()+" "+s.getAddress());
        });

        System.out.println("按照分数升序排序:");
        studentList.sort(Comparator.comparing(Student::getScore));
        studentList.forEach(s -> {
            System.out.println(s.getName()+" "+s.getScore()+" "+s.getHeight()+" "+s.getCollege()+" "+s.getAddress());
        });

        System.out.println("按照分数降序排序:");
        studentList.sort(Comparator.comparing(Student::getScore).reversed());
        studentList.forEach(s -> {
            System.out.println(s.getName()+" "+s.getScore()+" "+s.getHeight()+" "+s.getCollege()+" "+s.getAddress());
        });
    }
}

2、按照对象多属性升序,降序排序

public class Test {
    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("张三",90,180,"电气学院","北京"));
        studentList.add(new Student("李四",80,165,"计算机学院","上海"));
        studentList.add(new Student("王五",91,170,"财经学院","上海"));
        studentList.add(new Student("赵明",80,182,"计算机学院","北京"));
        studentList.add(new Student("钱海",75,181,"计算机学院","广州"));
        studentList.add(new Student("孙理",82,172,"财经学院","上海"));
        studentList.add(new Student("周伟",90,168,"电气学院","广州"));
        studentList.add(new Student("郑亮",80,178,"财经学院","广州"));
        System.out.println("原始数据:");
        studentList.forEach(s -> {
            System.out.println(s.getName()+" "+s.getScore()+" "+s.getHeight()+" "+s.getCollege()+" "+s.getAddress());
        });

        System.out.println("按照分数降序排序,当分数相同时, 按照身高升序排序");
        studentList.sort(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getHeight));
        studentList.forEach(s ->{
          System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());
        });
    }
}