Hello everyone,

Greetings today!

Today we will see how to sort Array List in ascending & descending order.

To sort in ascending order we need to use Collections.sort method and to sort a list in descending we need to use Collection.reverseOrder as shown below.

Let's look at how to sort lists in Java 8 in both ascending and descending order.

Now, let's look at how to sort an array list of custom objects in java and java 8.

For that let's create an Employee class and add a constructor & getter-setter.

Now we must create a class that will implement Comparator; currently, I am demonstrating how to sort employees based on salary.

Let us now look at how to sort. As we are sorting by salary, we will pass SalaryComparator.

 O/P

[Employee [employeeId=3, employeeName=Manoj, employeeSalary=29000],

Employee [employeeId=1, employeeName=Rajesh, employeeSalary=33000], 

Employee [employeeId=2, employeeName=Pritesh, employeeSalary=35000],

Employee [employeeId=4, employeeName=Santosh, employeeSalary=60000]]

Now, let's look at how to sort an array list of a custom object with different properties like salary and name in Java 8.

Here is output

Thanks 

Enjoy your learning!

Another post you can refer to is

Sorting HashMap By Key And Value + Java 8

Sorting HashSet In Ascending & Descending order + Java 8

Comparator and Comparable In Java + Difference Between Comparator and Comparable + Java 8

Java 8 Stream - Intermediate Operations With Examples

Java 8 Stream - Terminal Operations With Examples

For spring refer to Spring Tutorial