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
Create comparator class and override compare methodpackage 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; } }
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
Output: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); } }
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
Output: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); } }
Before Sort: [Rahul: 45, Sameera: 95, Amith: 28, Anju: 60, Lahiru: 86]
After Sort: [Sameera: 95, Lahiru: 86, Anju: 60, Rahul: 45, Amith: 28]
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletePython Training in electronic city
DataScience with Python Training in electronic city
AWS Training in electronic city
Big Data Hadoop Training in electronic city
Devops Training in electronic city
blockchain Training in electronic city
Hibernate Training in electronic city