How to sort Arraylist consisting of pojo class in java

I have a POJO Student class like this

class Student
{
    private int score;
    private String FirstName;
    //Getters and setters .................
}

I create an ArrayList as follows

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);
}

Now I want to sort it based on ratings in descending order ie Exit

1)def      95
2)abc      90
3)xyz      85
+4
source share
6 answers

You can use custom Comparator.

Here's a complete example (import exception):

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);
        }
    }
}

Output

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
]

Note

As already mentioned, you can also implement Comparable<Student>in your class Student, if only the default (or default) sort will be by result. #

Second edit

To sort in descending order, you can replace the operator returnin the Comparatorfollowing way:

return ((Integer)s2.getScore()).compareTo(s1.getScore());

programminglover , / !

+8

Comparator:

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

, Comparable:

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

:

Collections.sort(al_students);
+9

Java 8,

al_students.sort(Comparator.comparingInt(Student::getScore).reversed());
+4
0

, .
- , . , . , Comparable Comparator , , .

0

....

: Comparable Comparator, , , , Comparable Comparator.

.

0

All Articles