How to sort a ArrayList

We can sort an ArrayList By using Collections.sort() method.what if i want to sort employee object based on their employee id, for this we can pass Comparator object to sort method  which contains our sort logic.
below example will sort Employee based on employee id in ascending order.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ListSortExample {
     
    public static void main(String a[]){
         
        List<Employee> list = new ArrayList();
        
        list.add(new Employee("B",4));
        list.add(new Employee("A",2));
        list.add(new Employee("D",1));
        list.add(new Employee("C",3));
        
        System.out.println("Before Sorting: ");
        for(Employee e:list){
            System.out.println(e);
        }
        
        Collections.sort(list,new MyIdComp());
        System.out.println("After Sorting: ");
        for(Employee e:list){
            System.out.println(e);
        }
    }
}
 
class MyIdComp implements Comparator<Employee>{
 
   
    public int compare(Employee e1, Employee e2) {
        if(e1.getID() > e2.getID()){
            return 1;
        } else {
            return -1;
        }
    }
}
 
class Employee{
     
    private String name;
    private int id;
     
    public Employee(String n, int id){
        this.name = n;
        this.id = id;
    }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getID() {
        return id;
    }
    public void setID(int id) {
        this.id = id;
    }
    public String toString(){
        return "Name: "+this.name+" <<==>>ID: "+this.id;
    }
}

 

Output:

Before Sorting: 
Name: B <<==>>ID: 4
Name: A <<==>>ID: 2
Name: D <<==>>ID: 1
Name: C <<==>>ID: 3
After Sorting: 
Name: D <<==>>ID: 1
Name: A <<==>>ID: 2
Name: C <<==>>ID: 3
Name: B <<==>>ID: 4
sorting employee object based on employee name.
package test.collection;

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

public class ListSortExample {
     
    public static void main(String a[]){
         
        List<Employee> list = new ArrayList();
        
        list.add(new Employee("B",4));
        list.add(new Employee("A",2));
        list.add(new Employee("D",1));
        list.add(new Employee("C",3));
        
        System.out.println("Before Sorting: ");
        for(Employee e:list){
            System.out.println(e);
        }
        
        Collections.sort(list,new MyIdComp());
        System.out.println("After Sorting: ");
        for(Employee e:list){
            System.out.println(e);
        }
    }
}
 
class MyIdComp implements Comparator<Employee>{
 
   
    public int compare(Employee e1, Employee e2) {
       
        return e1.getName().compareTo(e2.getName());
    }
}
 
class Employee{
     
    private String name;
    private int id;
     
    public Employee(String n, int id){
        this.name = n;
        this.id = id;
    }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getID() {
        return id;
    }
    public void setID(int id) {
        this.id = id;
    }
    public String toString(){
        return "Name: "+this.name+" <<==>>ID: "+this.id;
    }
}

Output:

Before Sorting: 
Name: B <<==>>ID: 4
Name: A <<==>>ID: 2
Name: D <<==>>ID: 1
Name: C <<==>>ID: 3
After Sorting: 
Name: A <<==>>ID: 2
Name: B <<==>>ID: 4
Name: C <<==>>ID: 3
Name: D <<==>>ID: 1

Now sorting employee object based on name and Id Using a chained Comparator

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

public class EmployeeChainedComparator implements Comparator<Employee> {
 
    private List<Comparator<Employee>> listComparators;
 
    
    public EmployeeChainedComparator(Comparator<Employee>... comparators) {
        this.listComparators = Arrays.asList(comparators);
    }
 
    
    public int compare(Employee emp1, Employee emp2) {
        for (Comparator<Employee> comparator : listComparators) {
            int result = comparator.compare(emp1, emp2);
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }
}

the above comparator takes a list of comparators passed via its constructor; and the compare() method iterates over this comparators list to compare two Employee objects by each individual comparator. Now, we create separate comparators for each field by which we want to sort: id,name etc.

Name Comparator:

import java.util.Comparator;

public class EmployeeNameComparator implements Comparator<Employee> {
 
    
    public int compare(Employee emp1, Employee emp2) {
        return emp1.getName().compareTo(emp2.getName());
    }
}

Id Comparator:

public class EmployeeIdComparator implements Comparator {
 
   
    public int compare(Employee emp1, Employee emp2) {
        return emp1.getID() - emp2.getID();
    }
}

comparing using id comparator and name comparator.

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

public class ListSortMultipleAttribute {

    
      public static void main(String a[]){
             
          List<Employee> list = new ArrayList();
            
            list.add(new Employee("B",4));
            list.add(new Employee("A",2));
            list.add(new Employee("D",1));
            list.add(new Employee("C",3));
            
            System.out.println("Before Sorting: ");
            for(Employee e:list){
                System.out.println(e);
            }
            
            Collections.sort(list,new EmployeeChainedComparator(
             new EmployeeIdComparator(),new EmployeeNameComparator()));
            System.out.println("After Sorting: ");
            for(Employee e:list){
                System.out.println(e);
            }
        }
    
    
}
Advertisements
This entry was posted in Java. Bookmark the permalink.

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