Searching...
Thursday, 3 July 2014

How to sort list of objects with multiple attributes?

09:42

We can sort list of objects using Collections.sort(Collection c) or Collections.sort(Collection c,Comparator) based on compareTo,compare implementations. Some scenarios we need to sort objects based on multiple attributes. Here is one simple example to sort student objects based on the department, name and address using apache common api.

Create a Maven project and add below dependency.

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>

Create a pojo class (Student.java)

package com.test.vinod;

public class Student {
private String name;
private String address;
private String dept;

public Student(String dept, String name, String address) {
super();
this.dept = dept;
this.name = name;
this.address = address;
}

//getters and setters

@Override
public String toString() {
return dept + " " + name + " "
+ address;
}

}

StudentComparator.java

package com.test.vinod;

import java.util.Comparator;

import org.apache.commons.lang3.builder.CompareToBuilder;

public class StudentComparator implements Comparator<Student> {

public int compare(Student o1, Student o2) {
return new CompareToBuilder().append(o1.getDept(), o2.getDept())
.append(o1.getName(), o2.getName())
.append(o1.getAddress(), o2.getAddress()).toComparison();
}

}

Main class (StudentSort.java)

package com.test.vinod;

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

public class StudentSort {

public static void main(String[] args) {
Student s1 = new Student("Commerce", "Vinod", "Bangalore");
Student s2 = new Student("Electronics", "Ram", "Chennai");
Student s3 = new Student("Electronics", "Vinod", "Bangalore");
Student s4 = new Student("Commerce", "Nirmal", "Bombay");
Student s5 = new Student("Electronics", "Ashok", "Delhi");
Student s6 = new Student("Electronics", "Prem", "Agra");
Student s7 = new Student("Commerce", "Anoop", "Bangalore");
Student s8 = new Student("Electronics", "Santhosh", "Cochin");
Student s9 = new Student("Commerce", "Vinod", "Agra");

List<Student> studentList = new ArrayList<Student>();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
studentList.add(s5);
studentList.add(s6);
studentList.add(s7);
studentList.add(s8);
studentList.add(s9);

System.out.println("Before sorting:");
for (Student st : studentList) {
System.out.println(st);
}
Collections.sort(studentList, new StudentComparator());

System.out.println("\n\nAfter sorting:");
for (Student st : studentList) {
System.out.println(st);
}

}

}

Output

Before sorting:
Commerce Vinod Bangalore
Electronics Ram Chennai
Electronics Vinod Bangalore
Commerce Nirmal Bombay
Electronics Ashok Delhi
Electronics Prem Agra
Commerce Anoop Bangalore
Electronics Santhosh Cochin
Commerce Vinod Agra

After sorting:
Commerce Anoop Bangalore
Commerce Nirmal Bombay
Commerce Vinod Agra
Commerce Vinod Bangalore
Electronics Ashok Delhi
Electronics Prem Agra
Electronics Ram Chennai
Electronics Santhosh Cochin
Electronics Vinod Bangalore

0 comments: