CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Sort List of Objects in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Sort List of Objects in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: July 18, 2022 ยท 3 min read ยท 0 Comments
Share: in X
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.

Related Posts:

  • Pagination and Sorting in Spring Boot
  • MySQL Commands for Developers
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Write User Stories and Acceptance Criteria: A…
  • java.lang.Short Class in Java
  • MongoDB Document to POJO Class in Java
Tags:javajava8
Was this article helpful?
โ† Previous ArticleConnect MongoDB Atlas Cluster Using Shell
Next Article โ†’How to implement Factory Method Design Pattern in Spring Boot?

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap