Thursday, 16 April 2015

How to sort Java Objects using Comparable / Comparator Interface

There are two ways for sorting an object.

Here, is the quick reference
1) Comparable we use for natural sorting
2) Comparator for no natural sort order

Lets see both the examples one bye one

1) Comparable Example

package comparable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Student implements Comparable<Student> {

private int studentId;

//Constructor with one argument
public Student(int studentId) {
this.studentId = studentId;
}

public int compareTo(Student anotherInstance) {
return this.studentId - anotherInstance.studentId;
}

public static void main(String args[]) {

//Lets create a student list with random numbers
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student(5));
studentList.add(new Student(1));
studentList.add(new Student(3));
studentList.add(new Student(4));
studentList.add(new Student(2));

//Sorting of Student object based on studentId
Collections.sort(studentList);

System.out.println("Sorting Result (Based on studentId)");
//Display Student in ascending order
for(Student student: studentList) {
System.out.println(student.studentId);
}
}
}

Here is the output of above program execution:

Output
Sorting Result (Based on studentId)
1
2
3
4

5

2) Comparator Example

package comparator;

import comparator.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Student {

    private int weight;
    private int height;

    public Student(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }
   
public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}


public static void main(String[] args) {

//Dummy List of Students
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student(60,5));
studentList.add(new Student(50,6));
studentList.add(new Student(55,4));
studentList.add(new Student(65,6));
studentList.add(new Student(70,5));

// Sort by weight:
Collections.sort(studentList, new StudentWeightComparator());

System.out.println("Sorting based on Weight");
//Display Student in ascending order based on weight
for(Student student: studentList) {
System.out.println(student.getWeight() + "," + student.getHeight());
}

// Sort by height:
Collections.sort(studentList, new StudentHeightComparator());

System.out.println("Sorting based on Height");
//Display Student in ascending order based on height
for(Student student: studentList) {
System.out.println(student.getWeight() + "," + student.getHeight());
}

//In line comparator
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Student student1, Student student2) {
return student1.getWeight() - student2.getWeight();
}
});


System.out.println("Inline comparator for Sorting (Based on Weight)");
//Display Student in ascending order based on weight
for(Student student: studentList) {
System.out.println(student.getWeight() + "," + student.getHeight());
}

}
}

//Student Weight Comparator
class StudentWeightComparator implements Comparator<Student> {
    public int compare(Student student1, Student student2) {
        return student1.getWeight() - student2.getWeight();
    }
}

//Student Height Comparator
class StudentHeightComparator implements Comparator<Student> {
    public int compare(Student student1, Student student2) {
        return student1.getHeight() - student2.getHeight();
    }
}

Output
Sorting based on Weight
50,6
55,4
60,5
65,6
70,5
Sorting based on Height
55,4
60,5
70,5
50,6
65,6
Inline comparator for Sorting (Based on Weight)
50,6
55,4
60,5
65,6

70,5

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics  

No comments:

Post a Comment