Sort List Of Objects In Java

Sort List of Objects in Java

In this short post, we will learn to sort a List of Objects in Java. First, we will create a list of employees and sort employees based on their salary. So that, a user can easily identify who is getting the higher and lower salary.

Create an Employee class

public class Employee {
	private String name;
	private double salary;

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

	public String getName() {
		return name;
	}

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

	public double getSalary() {
		return salary;
	}

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

	@Override
	public String toString() {
		return "Employee [name=" + name + ", salary=" + salary + "]";
	}
}

Create Employee List

List<Employee> employeeList = Arrays.asList(new Employee("Radha Krishnan", 51000),
				new Employee("Virat Kohli", 45000), new Employee("Sachin Tendulkar", 50000),
				new Employee("Rohit Sharma", 49000));

Printing employees without sorting

System.out.println("Employees before sorting");
		employeeList.forEach(employee -> System.out.println(employee.getName() + " " + employee.getSalary()));

The output is:

Employees before sorting
Radha Krishnan 51000.0
Virat Kohli 45000.0
Sachin Tendulkar 50000.0
Rohit Sharma 49000.0

Ascending sort

List<Employee> sortedEmployeeList = employeeList.stream().sorted(Comparator.comparing(Employee::getSalary))
				.collect(Collectors.toList());

		System.out.println("Employees after sorting");
		sortedEmployeeList.forEach(employee -> System.out.println(employee.getName() + " " + employee.getSalary()));

Output:

Employees after sorting
Virat Kohli 45000.0
Rohit Sharma 49000.0
Sachin Tendulkar 50000.0
Radha Krishnan 51000.0

Here, the default sort order is ascending.

Descending sort

List<Employee> reversedEmployeeList = employeeList.stream()
				.sorted(Comparator.comparing(Employee::getSalary).reversed()).collect(Collectors.toList());

		System.out.println("Employees after sorting descending");
		reversedEmployeeList.forEach(employee -> System.out.println(employee.getName() + " " + employee.getSalary()));

Output:

Employees after sorting descending
Radha Krishnan 51000.0
Sachin Tendulkar 50000.0
Rohit Sharma 49000.0
Virat Kohli 45000.0

You can see in the output before sorting and after sorting that the employee name is printing differently.

Before sorting the employee, all the employee data is printed based on the insertion order in the employeeList.

After sorting the employeeList the employee data is printed in ascending order of their salary. This basically means, the employee who is getting the lowest salary is printed first and the highest salaried employee is printed last.

Similarly, when we used the reversed() method then it was printed in descending order by the employee’s salary.

Complete example

You can see the complete example code below:

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

public class EmployeeSortDemo {
	public static void main(String[] args) {
		List<Employee> employeeList = Arrays.asList(new Employee("Radha Krishnan", 51000),
				new Employee("Virat Kohli", 45000), new Employee("Sachin Tendulkar", 50000),
				new Employee("Rohit Sharma", 49000));
		System.out.println("Employees before sorting");
		employeeList.forEach(employee -> System.out.println(employee.getName() + " " + employee.getSalary()));

		List<Employee> sortedEmployeeList = employeeList.stream().sorted(Comparator.comparing(Employee::getSalary))
				.collect(Collectors.toList());

		System.out.println("Employees after sorting");
		sortedEmployeeList.forEach(employee -> System.out.println(employee.getName() + " " + employee.getSalary()));

		List<Employee> reversedEmployeeList = employeeList.stream()
				.sorted(Comparator.comparing(Employee::getSalary).reversed()).collect(Collectors.toList());

		System.out.println("Employees after sorting descending");
		reversedEmployeeList.forEach(employee -> System.out.println(employee.getName() + " " + employee.getSalary()));
	}
}

Summary

In this post, we learned to sort the list of objects in Java by ascending and descending order.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments