Java 8 Lambda - Sort List in Ascending and Descending Order | Comparator Example

In this tutorial, we will see how to sort List (ArrayList) in ascending and descending order using Java Lambda expressions.
Let's start with the basics and traditional ways of sorting List (ArrayList) in ascending and descending.
Learn Java Lambda at https://www.javaguides.net/2018/07/java-8-lambda-expressions.html.

Sort List of integers using Collections.sort() method

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

public class SortList {

    public static void main(String[] args) {

        // create list
        List < Integer > list = new ArrayList < Integer > ();
        list.add(10);
        list.add(30);
        list.add(20);
        list.add(50);
        list.add(40);

        Collections.sort(list); // ascending order 
        System.out.println(list);

        Collections.reverse(list); // descending order 
        System.out.println(list);
    }
}
Output:
[10, 20, 30, 40, 50]
[50, 40, 30, 20, 10]

Sort List of Employee Objects in Ascending and Descending Order using Comparator

Let's create an Employee model with the following fields:
package com.java.tutorials.sorting;

public class Employee {
    private int id;
    private String name;
    private int age;
    private long salary;
    public Employee(int id, String name, int age, long salary) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public long getSalary() {
        return salary;
    }
    public void setSalary(long salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
    }
}
Now, let's create a class MySort which implements Comparator interface and provide logic to sort Employee by salary:
class MySort implements Comparator < Employee > {

    @Override
    public int compare(Employee o1, Employee o2) {
        return (int)(o1.getSalary() - o2.getSalary());
    }
}
Now, let's see how to sort an Employee by salary using Collections.sort() method in ascending order:
package com.java.tutorials.sorting;

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

public class SortList {

    public static void main(String[] args) {

        List < Employee > employees = new ArrayList < Employee > ();
        employees.add(new Employee(10, "Ramesh", 30, 400000));
        employees.add(new Employee(20, "Santosh", 29, 350000));
        employees.add(new Employee(30, "Sanjay", 30, 450000));
        employees.add(new Employee(40, "Pramod", 29, 500000));

        // ascending order
        Collections.sort(employees, new MySort());

        System.out.println(employees);
    }
}
Output:
[Employee [id=20, name=Santosh, age=29, salary=350000], Employee [id=10, name=Ramesh, age=30, salary=400000], Employee [id=30, name=Sanjay, age=30, salary=450000], Employee [id=40, name=Pramod, age=29, salary=500000]]

On order to sort Employee by their salary in descending order, we need to change MySort class with the following changes:
class MySort implements Comparator < Employee > {

    @Override
    public int compare(Employee o1, Employee o2) {
        return (int)(o2.getSalary() - o1.getSalary());
    }
}
Note that we have done a single line of change to sort Employee by their salary in descending order.
 return (int)(o2.getSalary() - o1.getSalary());

Sort List of Employee Objects in Ascending and Descending Order using Lambda Expressions

In this example, we will see how to sort a list of employees by name in ascending and descending order using Lambda Expressions:
package com.java.tutorials.sorting;

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

public class SortList {

    public static void main(String[] args) {

        List < Employee > employees = new ArrayList < Employee > ();
        employees.add(new Employee(10, "Ramesh", 30, 400000));
        employees.add(new Employee(20, "Santosh", 29, 350000));
        employees.add(new Employee(30, "Sanjay", 30, 450000));
        employees.add(new Employee(40, "Pramod", 29, 500000));

        Collections.sort(employees, new Comparator < Employee > () {
            @Override
            public int compare(Employee o1, Employee o2) {
                return (int)(o1.getName().compareTo(o2.getName()));
            }
        });

        // using lambda expression
        // ascending order
        Collections.sort(employees, (o1, o2) - > (o1.getName().compareTo(o2.getName())));
        System.out.println("Ascending order => " + employees);
        // descending order
        Collections.sort(employees, (o1, o2) - > (o2.getName().compareTo(o1.getName())));
        System.out.println("Descending order => " + employees);
        // using Comparator.comparing() method
        Collections.sort(employees, Comparator.comparing(Employee::getName));
    }

}

class MySort implements Comparator < Employee > {

    @Override
    public int compare(Employee o1, Employee o2) {
        return (int)(o1.getSalary() - o2.getSalary());
    }

}
Note that the lambda expression we used to sort List of employees:
 // using lambda expression
        // ascending order
        Collections.sort(employees, (o1, o2) - > (o1.getName().compareTo(o2.getName())));
        System.out.println("Ascending order => " + employees);
        // descending order
        Collections.sort(employees, (o1, o2) - > (o2.getName().compareTo(o1.getName())));
        System.out.println("Descending order => " + employees);
Output:
Ascending order => [Employee [id=40, name=Pramod, age=29, salary=500000], Employee [id=10, name=Ramesh, age=30, salary=400000], Employee [id=30, name=Sanjay, age=30, salary=450000], Employee [id=20, name=Santosh, age=29, salary=350000]]
Descending order => [Employee [id=20, name=Santosh, age=29, salary=350000], Employee [id=30, name=Sanjay, age=30, salary=450000], Employee [id=10, name=Ramesh, age=30, salary=400000], Employee [id=40, name=Pramod, age=29, salary=500000]]

Related Java 8 Tutorials

Comments