Sorting an ArrayList that contains pojo class

sorting the arraylist contains pojo classes

Sorting an ArrayList that contains pojo class

Sorting of ArrayList containing our own Class/Objects in java, Sorting ArrayList that contains customizes java class/objects in java . How to sort Arraylist that contains our own class in java?
Here is the example of sorting  a list that contains java pojo class.
In below example, we have Employee class with attribute id, first name & last name.
We have another class that actually use the sort() method of Collections which has to implement Comparator() to sort pojo with its field.

Here is the Employee Class


  1. public class Employee   
  2. {  
  3.     int id;  
  4.     String firstName;  
  5.     String lastName;  
  6.       
  7.     public Employee(int id,String firstName,String lastName)  
  8.     {  
  9.         this.id=id;  
  10.         this.firstName=firstName;  
  11.         this.lastName=lastName;  
  12.     }  
  13.     public int getId() {  
  14.         return id;  
  15.     }  
  16.     public void setId(int id) {  
  17.         this.id = id;  
  18.     }  
  19.     public String getFirstName() {  
  20.         return firstName;  
  21.     }  
  22.     public void setFirstName(String firstName) {  
  23.         this.firstName = firstName;  
  24.     }  
  25.     public String getLastName() {  
  26.         return lastName;  
  27.     }  
  28.     public void setLastName(String lastName) {  
  29.         this.lastName = lastName;  
  30.     }  
  31. }  

Here is actual class that sort the list which contains Employee objects

  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. import java.util.Comparator;  
  4. import java.util.List;  
  5.   
  6.   
  7. public class SortEmployeeList  
  8. {  
  9.     public static void main(String[] args)   
  10.     {  
  11.         Employee emp1 = new Employee(1,“Dipen”,“Khadka”);  
  12.         Employee emp2 = new Employee(2,“John”,“Rotela”);  
  13.         Employee emp3 = new Employee(3,“Shark”,“Anderson”);  
  14.         Employee emp4 = new Employee(4,“Suman”,“Bhatt”);  
  15.           
  16.         List<Employee> employeeList = new ArrayList<Employee>();  
  17.         employeeList.add(emp1);  
  18.         employeeList.add(emp2);  
  19.         employeeList.add(emp3);  
  20.         employeeList.add(emp4);  
  21.           
  22.         //sort by First name  
  23.         Collections.sort(employeeList, new Comparator() {  
  24.             @Override  
  25.             public int compare(Object obj1, Object obj2) {  
  26.                 Employee emp1 = (Employee)obj1;  
  27.                 Employee emp2 = (Employee)obj2;  
  28.                 return emp1.getFirstName().compareToIgnoreCase(emp2.getFirstName());  
  29.             }  
  30.         });  
  31.         //Now the employeeList is sorted based on first name  
  32.           
  33.           
  34.         //sort by Last name  
  35.         Collections.sort(employeeList, new Comparator() {  
  36.             @Override  
  37.             public int compare(Object obj1, Object obj2) {  
  38.                 Employee emp1 = (Employee)obj1;  
  39.                 Employee emp2 = (Employee)obj2;  
  40.                 return emp1.getLastName().compareToIgnoreCase(emp2.getLastName());  
  41.             }  
  42.         });  
  43.         //Now the employeeList is sorted based on last name  
  44.     }  
  45. }  
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: