27
Feb
Sort collection of objects with Comparable and Comparator
Sort collection of objects with Comparable and Comparator
===============================================================
Source Class:
=============
package Package2;
import java.util.Comparator;
import java.util.Date;
public class Student implements Comparable<Student> {
private int rollNo;
private String name;
private Date doj;
public int getRollNo() {
return rollNo;
}
public String getName() {
return name;
}
public Date getDoj() {
return doj;
}
public Student(int rollNo, String name, Date doj) {
super();
this.rollNo = rollNo;
this.name = name;
this.doj = doj;
}
@Override
public int compareTo(Student o) {
return this.getRollNo() - o.getRollNo(); // Comparable implemented sort
}
// Comparators
static Comparator<Student> getNameComparator = Comparator.comparing(Student::getName);
static Comparator<Student> getRollNoComparator = Comparator.comparing(Student::getRollNo);
static Comparator<Student> getDojComparator = Comparator.comparing(Student::getDoj);
// Complex Comparators
static Comparator<Student> complexComparator1 = Comparator.comparing(Student::getName)
.thenComparing(Comparator.comparing(Student::getRollNo))
.thenComparing(Comparator.comparing(Student::getDoj));
}
Definition of arrL in psv Main()
====================================
public static void main(String[] args) {
ArrayList<Student> arrL = new ArrayList<Student>();
arrL.add(new Student(9, "A", new Date(2020, 01, 17)));
arrL.add(new Student(4, "C", new Date(2020, 01, 16)));
arrL.add(new Student(8, "B", new Date(2020, 01, 18)));
for (Student s : arrL)
System.out.println("Name " + "=>" + s.getName() + " RollNo " + "=>" + s.getRollNo() + " Doj " + "=>"
+ s.getDoj() + " ## ");
Collections.sort(arrL, new Comparator<Student>() {
public int compare(Student one, Student two) {
return one.getName().compareTo(two.getName());
}
});
System.out.println("\n== After sort ==");
for (Student s : arrL)
System.out.println("Name " + "=>" + s.getName() + " RollNo " + "=>" + s.getRollNo() + " Doj " + "=>"
+ s.getDoj() + " ## ");
}
Ways to sort Student objects:
==============================
##1. As per sort order of Comparable implementation ======================================
Collections.sort(arrL);
Collections.sort(arrL, Collections.reverseOrder());
##2. Dynamic Comparator implementation ======================================
Collections.sort(arrL, Comparator.comparing(Student::getName));
Collections.sort(arrL, Comparator.comparing(Student::getName).reversed());
##3. Using Static Comparators defined in Student Clas ======================================
Collections.sort(arrL, Student.getNameComparator);
Collections.sort(arrL, Student.getNameComparator.reversed());
##4. Using inline comparators ======================================
Collections.sort(arrL, new Comparator<Student>() {
public int compare(Student one, Student two) {
return one.getName().compareTo(two.getName());
}
});
http://defectracker.com/wp-content/uploads/2020/02/Sorting-with-Comparable-and-Comparators.txt
0 comments