如何在java中对由pojo类组成的Arraylist进行排序

pro*_*ver 4 java sorting arraylist

我有这样的POJO班学生

class Student
{
    private int score;
    private String FirstName;
    //Getters and setters .................
}
Run Code Online (Sandbox Code Playgroud)

我正在创建像这样的ArrayList

public static void main(String[] args)
{
    List<Student> al_students= new ArrayList<Student>();
    Student s1= new Student();
    s1.setScore(90);
    s1.setFirstName("abc");
    al_students.add(s1);

    Student s2= new Student();
    s2.setScore(95);
    s2.setFirstName("def");
    al_students.add(s2);

    Student s3= new Student();
    s3.setScore(85);
    s3.setFirstName("xyz");
    al_students.add(s3);
}
Run Code Online (Sandbox Code Playgroud)

现在我想根据降序排列即
输出来对其进行排序

1)def      95
2)abc      90
3)xyz      85
Run Code Online (Sandbox Code Playgroud)

Glo*_*del 10

使用比较器:

    Collections.sort(al_students, new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            return Integer.compare(o2.getScore(), o1.getScore());
        }           
    });
Run Code Online (Sandbox Code Playgroud)

或者,让Student实现Comparable接口:

class Student implements Comparable<Student> {
    ...
    @Override
    public int compareTo(Student s) {
        return Integer.compare(s.getScore(), getScore());
    }
}
Run Code Online (Sandbox Code Playgroud)

那么你可以在没有比较器的情况下进行排序:

Collections.sort(al_students);
Run Code Online (Sandbox Code Playgroud)


Men*_*ena 9

您可以使用自定义Comparator.

这是一个完整的例子(不包括导入):

public class Main {

    // main method setting up and printing students
    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();
        Student s1 = new Student();
        s1.setScore(90);
        s1.setFirstName("abc");
        students.add(s1);

        Student s2 = new Student();
        s2.setScore(95);
        s2.setFirstName("def");
        students.add(s2);

        Student s3 = new Student();
        s3.setScore(85);
        s3.setFirstName("xyz");
        students.add(s1);
        System.out.printf("Unordered: %s%n", students);
        // sorting using anonymous Comparator
        Collections.sort(students, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                // notice the cast to (Integer) to invoke compareTo
                return ((Integer)s1.getScore()).compareTo(s2.getScore());
            }
        });
        System.out.printf("Ordered: %s%n", students);
    }
    // Student class
    static class Student {
        private int score;
        private String firstName;
        // boring stuff
        public int getScore() {
            return score;
        }

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

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String name) {
            this.firstName = name;
        }
        // for printing
        @Override
        public String toString() {
            return String.format("Student \"%s\" with score: %d%n", firstName,
                    score);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

Unordered: [Student "abc" with score: 90
, Student "def" with score: 95
, Student "abc" with score: 90
]
Ordered: [Student "abc" with score: 90
, Student "abc" with score: 90
, Student "def" with score: 95
]
Run Code Online (Sandbox Code Playgroud)

注意

正如其他人所提到的,如果唯一(或默认)排序将按分数排序,您也可以implement Comparable<Student>Student课堂上.

第二次编辑

为了按递减顺序排序,您可以使用以下内容替换您的return语句Comparator:

return ((Integer)s2.getScore()).compareTo(s1.getScore());
Run Code Online (Sandbox Code Playgroud)

感谢编程人员发现错误拒绝编辑的这个/道歉!


Psh*_*emo 6

如果您使用的是 Java 8,那么您的代码可能如下所示

al_students.sort(Comparator.comparingInt(Student::getScore).reversed());
Run Code Online (Sandbox Code Playgroud)