- In order to compare objects we have comparable and comparator in java.
- If you want to do custom sorting we will use comparator in java
- We need to use different comparators for sorting objects by different fields.
- And using Collections.sort(List, Comparator).
- We can sort list of class objects by using cmparator.
- By this we can compare two objects field by field. actually many.
- Lets see an example program to sort list of student objects by name rollno and marks using comparator. Here comparator means we need to develop sorting logic in separate class which implements comparator interface and overrides compare() method.
Student class:
- Define a class as student add variables.
- Define a constructor to assign values to variables.
- Define a toString() method to print each variable value inside object values when we print object.
- package com.sortobjects;
- /**
- * How to sort list of class objects
- * @author www.instanceofjava.com
- */
- public class Student {
- String name;
- int Rollno;
- float marks;
- Student(String name, int Rollno, float marks){
- this.name=name;
- this.marks=marks;
- this.Rollno=Rollno;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getRollno() {
- return Rollno;
- }
- public void setRollno(int rollno) {
- Rollno = rollno;
- }
- public float getMarks() {
- return marks;
- }
- public void setMarks(float marks) {
- this.marks = marks;
- }
- public String toString() {
- return ("Name:"+name+"\tRollNo:"+Rollno+"\tMarks"+marks);
- }
- }
NameComparator:
- This class sort list of student class objects by name.
- package com.sortobjects;
- import java.util.Comparator;
- public class NameComparator implements Comparator<Student>{
- /**
- * How to sort list of class objects
- * @author www.instanceofjava.com
- */
- @Override
- public int compare(Student obj1, Student obj2) {
- return obj1.getName().compareTo(obj2.getName());
- }
- }
RollNoComparator :
- This class sort list of student class objects by Rollno.
- package com.sortobjects;
- import java.util.Comparator;
- public class RollNoComparator implements Comparator<Student>{
- /**
- * How to sort list of class objects
- * @author www.instanceofjava.com
- */
- @Override
- public int compare(Student obj1, Student obj2) {
- return ((Integer)obj1.getRollno()).compareTo((Integer)obj2.getRollno());
- }
- }
MarksComparator:
- This class will compare list of student class objects by marks
- package com.sortobjects;
- import java.util.Comparator;
- public class MarksComparator implements Comparator<Student>{
- /**
- * How to sort list of class objects
- * @author www.instanceofjava.com
- */
- @Override
- public int compare(Student obj1, Student obj2) {
- return ((Float)obj1.getMarks()).compareTo((Float)obj2.getMarks());
- }
- }
SortListObjects:
- Take a test class
- Create arraylist object and add Student objects with different values into list.
- Using Collections.Sort(List,FiledComparator) pass corresponding comparator class in order to sort multiple fields of a class.
- package com.sortobjects;
- mport java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- /**
- * How to sort list of class objects
- * @author www.instanceofjava.com
- */
- public class SortListObjects {
- public static void main(String[] args){
- List<Student> studentlst= new ArrayList<Student>();
- studentlst.add(new Student("Saisesh",1,80));
- studentlst.add(new Student("Vinod",2,90));
- studentlst.add(new Student("Ajay",3,95));
- System.out.println("** Before sorting **:");
- for (Student student : studentlst) {
- System.out.println(student);
- }
- Collections.sort(studentlst,new NameComparator());
- System.out.println("** After sorting **");
- for (Student student : studentlst) {
- System.out.println(student);
- }
- }
- }
- Like this we can compare list of objects by Rollno, and marks aslo. Please practice this example by sorting rollno and marks and check output. if you have any doubts then leave a comment.
Chained comparator:
- We can use chained comparator to sort list of class objects by multiple fields by passing multiple comparators.
- java collections sort multiple comparators
- The below program is example of chained comparator java
- package com.sortobjects;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.List;
- public class StudentChainedComparator implements Comparator<Student> {
- private List<Comparator<Student>> listComparators;
- public StudentChainedComparator(Comparator<Student>... comparators) {
- this.listComparators = Arrays.asList(comparators);
- }
- @Override
- public int compare(Student student1, Student student2) {
- for (Comparator<Student> comparator : listComparators) {
- int result = comparator.compare(student1, student2);
- if (result != 0) {
- return result;
- }
- }
- return 0;
- }
- }
Java comparator multiple fields example:
- Lets see an example of java comparatorchain in java
No comments