Sorting an ArrayList that contains pojo class:
Please go through the below example.
First create a new java class and named as Employee.
package com.sana.collections;
public class Employee {
int id;
String firstName;
String lastName;
public Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + '}';
public class Employee {
int id;
String firstName;
String lastName;
public Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + '}';
// return firstName ;
}
}
}
}
Actual Class for Testing:
Create a new java class named as SortListTest:
package com.sana.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class MyComparator implements Comparator<Employee> {
public int compare(Employee emp1, Employee emp2) {
// comparing Firstname from both objects @Sorting based on First Name
return emp1.getFirstName().compareTo(emp2.getFirstName());
}
}
public class SortListTest {
public static void main(String[] args) {
Employee emp1 = new Employee(1, "SHEKAR", "SANA");
Employee emp2 = new Employee(2, "CHIRU", "ALLEM");
Employee emp3 = new Employee(3, "CHANDRA", "SHEKAR");
Employee emp4 = new Employee(4, "Test", "Test");
Employee emp5 = new Employee(4, "Mahesh", "Burugu");
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
Collections.sort(employeeList, new MyComparator());
for (Employee e : employeeList) {
System.out.println(e);
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class MyComparator implements Comparator<Employee> {
public int compare(Employee emp1, Employee emp2) {
// comparing Firstname from both objects @Sorting based on First Name
return emp1.getFirstName().compareTo(emp2.getFirstName());
}
}
public class SortListTest {
public static void main(String[] args) {
Employee emp1 = new Employee(1, "SHEKAR", "SANA");
Employee emp2 = new Employee(2, "CHIRU", "ALLEM");
Employee emp3 = new Employee(3, "CHANDRA", "SHEKAR");
Employee emp4 = new Employee(4, "Test", "Test");
Employee emp5 = new Employee(4, "Mahesh", "Burugu");
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
Collections.sort(employeeList, new MyComparator());
for (Employee e : employeeList) {
System.out.println(e);
}
}
}
OUTPUT:
Employee{id=3, firstName=CHANDRA, lastName=SHEKAR}
Employee{id=2, firstName=CHIRU, lastName=ALLEM}
Employee{id=4, firstName=Mahesh, lastName=Burugu}
Employee{id=1, firstName=SHEKAR, lastName=SANA}
Employee{id=4, firstName=Test, lastName=Test}
Employee{id=2, firstName=CHIRU, lastName=ALLEM}
Employee{id=4, firstName=Mahesh, lastName=Burugu}
Employee{id=1, firstName=SHEKAR, lastName=SANA}
Employee{id=4, firstName=Test, lastName=Test}
BY
SANA