How to Sort ArrayList of Object Type in Java? 

Collections class has a method Sort which is used to sort arraylist.

sort(List<T> list)
sort(List<T> list, Comparator<? super T> c)

 

If List contains String,int... we can use first method.

If the List contains Object type we have to use second method to sort List.

The Second method has parameter of type Comparator which compare the object by

compare(Object  o1, Object o2) method

 

Example

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

public class JavaApplication1 {

  
    
    public static void main(String[] args) {
        
        ArrayList<Employee>listEmp=new ArrayList();
        Employee emp1=new Employee("Name 1", 20000, "Associate Developer");
        listEmp.add(emp1);

        Employee emp2=new Employee("Name 2", 18000, "Junior Developer");
        listEmp.add(emp2);

         Employee emp3=new Employee("Name 3", 48000, "Manager");
        listEmp.add(emp3);

        Employee emp4=new Employee("Name 4", 30000, "TeamLead");
        listEmp.add(emp4);
        
        System.out.println("Before Sorting....");
        for(int k=0;k<listEmp.size();k++)
        System.out.println("Emp "+listEmp.get(k).name+" : Salary = "+listEmp.get(k).salary);
        
        Collections.sort(listEmp, new Comparator<Employee>() {

            @Override
            public int compare(Employee o1, Employee o2) {
                
                     if (o1.getSalary()==o2.getSalary()) {
                        return 0;
                     } else if (o1.getSalary()>o2.getSalary()) {
                        return 1;
                     } else {
                        return -1;
                     }
              
            }
        });
        
         System.out.println("After Sorting....");
        for(int k=0;k<listEmp.size();k++)
        System.out.println("Emp "+listEmp.get(k).name+" : Salary = "+listEmp.get(k).salary);
    }
    
    
}


class Employee {
        String name;
        int salary;
        String designation;

        public Employee(String name, int salary, String designation) {
            this.name = name;
            this.salary = salary;
            this.designation = designation;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }

        public String getDesignation() {
            return designation;
        }

        public void setDesignation(String designation) {
            this.designation = designation;
        }
        
    }

 

Output

Before Sorting....
Emp Name 1 : Salary = 20000
Emp Name 2 : Salary = 18000
Emp Name 3 : Salary = 48000
Emp Name 4 : Salary = 30000


After Sorting....
Emp Name 2 : Salary = 18000
Emp Name 1 : Salary = 20000
Emp Name 4 : Salary = 30000
Emp Name 3 : Salary = 48000

 


Subscribe For Daily Updates

Flutter Questions
Android Questions