Java sort an ArrayList of objects

Well sorting is an important part of any programming language. Most of the time you want your data displayed in a certain sequence, while you can retrieve data from tables in certain sequence what to do when it comes to a list of Java object. Well in that case you have two choices either the object that is in the list needs to implement the Comparable which basically forces you to override the compareTo() method or create a comparator that is capable of comparing two objects by overriding the compare() method. Here is an example of an ArrayList of an Employee objects that is sorted using the Collections.sort() method.

Java Source Code

package com.as400samplecode;

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

public class SortArrayList {
 
 public static void main(String[] args) {
  
  SortArrayList sortArrayList = new SortArrayList();
  sortArrayList.runExample();
  
 }
 
 public void runExample(){
  
  //create an ArrayList of employee objects
  ArrayList<Employee> employeeList = new ArrayList<Employee>();
  
  Employee employee1 = new Employee("id1", "John", 10900);
  employeeList.add(employee1);
  Employee employee2 = new Employee("id2", "Ed", 10100);
  employeeList.add(employee2);
  Employee employee3 = new Employee("id3", "Gary", 10200);
  employeeList.add(employee3);
  Employee employee4 = new Employee("id4", "Mike", 10700);
  employeeList.add(employee4);
  Employee employee5 = new Employee("id5", "Steve", 10300);
  employeeList.add(employee5);
  
  //define a custom comparator to compare employee salaries
  Comparator<Employee> comparator = new Comparator<Employee>(){
 
            public int compare(Employee emp1, Employee emp2) {
                 return (emp1.getSalary() - emp2.getSalary());
            }
 
        };
        
        //default sorting by name
        Collections.sort(employeeList);
  
        System.out.println("Sort by Employee Name --->"); 
  for(Employee employee: employeeList){
   System.out.println("Employee: " + 
     employee.getId() + ", " +
     employee.getName() + ", " +
     employee.getSalary());
  }
  
        //sort by salary using a custom comparator
        Collections.sort(employeeList, comparator);
  
        System.out.println("Sort by Employee Salary --->"); 
  for(Employee employee: employeeList){
   System.out.println("Employee: " + 
     employee.getId() + ", " +
     employee.getName() + ", " +
     employee.getSalary());
  }
        
 }
 
 public class Employee implements Comparable<Employee>{
  
  String id = "";
  String name = "";
  int salary = 0;
  
  public Employee(String id, String name, int salary) {
   super();
   this.id = id;
   this.name = name;
   this.salary = salary;
  }
  
  public String getId() {
   return id;
  }
  public void setId(String id) {
   this.id = id;
  }
  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;
  }

  @Override
  public int compareTo(Employee employee) {
   return this.getName().compareTo(employee.getName());
  }

 }

}

Output

Sort by Employee Name --->
Employee: id2, Ed, 10100
Employee: id3, Gary, 10200
Employee: id1, John, 10900
Employee: id4, Mike, 10700
Employee: id5, Steve, 10300
Sort by Employee Salary --->
Employee: id2, Ed, 10100
Employee: id3, Gary, 10200
Employee: id5, Steve, 10300
Employee: id4, Mike, 10700
Employee: id1, John, 10900

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.