Objects sorting in java

Here I explain how to sort java objects using Comparator and  Comparable interfaces.

We want to sort students by marks.
Rahul: 45,
Sameera: 95,
Amith: 28,
Anju: 60,
Lahiru: 86

Method 1: Comparator 


Now create student class

Student.java

package com.codestore.sorting;

/**
 * @author Sameera Jayasekara
 */
public class Student {
    String name;
    int totalMarks;

    public Student(String name, int totalMarks) {
        this.name = name;
        this.totalMarks = totalMarks;

    }

    @Override
    public String toString() {
        return name + ": " + totalMarks;
    }
}

Create comparator class and override compare method

StudentMarksComparator.java

import java.util.Comparator;

/**
 * @author Sameera Jayasekara
 */
public class StudentMarksComparator implements Comparator<Student> {

    @Override
    public int compare(Student s1, Student s2) {
        Student student1 = s1;
        Student student2 = s2;
        return student2.totalMarks - student1.totalMarks;
    }

}

Main.java

package com.codestore.sorting;

import java.util.*;

/**
 * @author Sameera Jayasekara
 */
public class Main {

    public static void main(String[] args) {

        List<Student> students = new ArrayList<Student>();
        students.add(new Student("Rahul", 45));
        students.add(new Student("Sameera", 95));
        students.add(new Student("Amith", 28));
        students.add(new Student("Anju", 60));
        students.add(new Student("Lahiru", 86));

        System.out.println("Before Sort: " + students);

        StudentMarksComparator smc = new StudentMarksComparator();
        Collections.sort(students, smc);

        System.out.println("After Sort: " + students);

    }

}
 
Output:
Before Sort: [Rahul: 45, Sameera: 95, Amith: 28, Anju: 60, Lahiru: 86]
After Sort: [Sameera: 95, Lahiru: 86, Anju: 60, Rahul: 45, Amith: 28]

You can directly add a comparator class as a method parameter without creating a separate class.
package com.codestore.sorting;

import java.util.*;

/**
 * @author Sameera Jayasekara
 */
public class Main {

 public static void main(String[] args) {

  List<Student> students = new ArrayList<Student>();
  students.add(new Student("Rahul", 45));
  students.add(new Student("Sameera", 95));
  students.add(new Student("Amith", 28));
  students.add(new Student("Anju", 60));
  students.add(new Student("Lahiru", 86));

  System.out.println("Before Sort: " + students);

  // StudentMarksComparator smc = new StudentMarksComparator();
  // Collections.sort(students, smc);

  Collections.sort(students, new Comparator<Student>() {

   @Override
   public int compare(Student s1, Student s2) {
    Student student1 = s1;
    Student student2 = s2;
    return student2.totalMarks - student1.totalMarks;
   }

  });

  System.out.println("After Sort: " + students);

 }

}

Method 2: Comparable


Student.java

package com.codestore.sorting.comparable;

/**
 * @author Sameera Jayasekara
 */
public class Student implements Comparable<Student> {

    String name;
    int totalMarks;

    public Student(String name, int totalMarks) {
        this.name = name;
        this.totalMarks = totalMarks;
    }

    @Override
    public int compareTo(Student s) {
        return this.totalMarks - s.totalMarks;
    }

    @Override
    public String toString() {
        return name + ": " + totalMarks;
    }

}
 

Main.java

package com.codestore.sorting.comparable;

import java.util.*;

/**
 * @author Sameera Jayasekara
 */
public class Main {

    public static void main(String[] args) {

        List<Student> students = new ArrayList<Student>();
        students.add(new Student("Rahul", 45));
        students.add(new Student("Sameera", 95));
        students.add(new Student("Amith", 28));
        students.add(new Student("Anju", 60));
        students.add(new Student("Lahiru", 86));

        System.out.println("Before Sort: " + students);

        Collections.sort(students);

        System.out.println("After Sort: " + students);

    }

}
 
Output:
Before Sort: [Rahul: 45, Sameera: 95, Amith: 28, Anju: 60, Lahiru: 86]
After Sort: [Sameera: 95, Lahiru: 86, Anju: 60, Rahul: 45, Amith: 28]

1 comment: