In this tutorial, we will see how to sort an array of objects in Ascending and descending order using Java 8 Lambda expressions.
Learn Java 8 Lambda at https://www.javaguides.net/2018/07/java-8-lambda-expressions.html
Also, check out Java Sort Array Objects using Comparable Interface.
Check out Comparator example at https://www.javaguides.net/2018/12/java-comparator-interface-example.html
Learn Java Array at https://www.javaguides.net/2020/04/java-array-tutorial-for-beginners.html.
Employee Class
Let's first create an Employee class:
package com.java.array.tutorial.sorting;
public class Employee implements Comparable < Employee > {
private int id;
private String name;
private int salary;
public Employee(int id, String name, int salary) {
super();
this.id = id;
this.name = name;
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 getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
}
@Override
public int compareTo(Employee o) {
return this.salary - o.salary;
}
}
Java 8 Lambda - Sort an Array of Employee objects (by salary) in Ascending Order
The following example sort an array of employee objects in Ascending order:
package com.java.array.tutorial.sorting;
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
// sort Array of employee objects using Comparator interface
Employee[] employees = {
new Employee(10, "Ramesh", 50000),
new Employee(20, "John", 30000),
new Employee(30, "tom", 40000),
new Employee(40, "Tony", 60000)
};
System.out.println(" Before sorting => " + Arrays.toString(employees));
Arrays.sort(employees, (o1, o2) -> o1.getSalary() - o2.getSalary());
System.out.println(" After sorting => " + Arrays.toString(employees));
}
}
Output:
Before sorting => [Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=40, name=Tony, salary=60000]]
After sorting => [Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=40, name=Tony, salary=60000]]
Java 8 Lambda - Sort an Array of Employee objects (by salary) in Descending Order
The following example sort an array of employee objects in descending order:
package com.java.array.tutorial.sorting;
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
// sort Array of employee objects using Comparator interface
Employee[] employees = {
new Employee(10, "Ramesh", 50000),
new Employee(20, "John", 30000),
new Employee(30, "tom", 40000),
new Employee(40, "Tony", 60000)
};
System.out.println(" Before sorting => " + Arrays.toString(employees));
Arrays.sort(employees, (o1, o2) -> o2.getSalary() - o1.getSalary());
System.out.println(" After sorting => " + Arrays.toString(employees));
}
}
Output:
Before sorting => [Employee [id=10, name=Ramesh, salary=50000], Employee [id=20, name=John, salary=30000], Employee [id=30, name=tom, salary=40000], Employee [id=40, name=Tony, salary=60000]]
After sorting => [Employee [id=40, name=Tony, salary=60000], Employee [id=10, name=Ramesh, salary=50000], Employee [id=30, name=tom, salary=40000], Employee [id=20, name=John, salary=30000]]
Also, check out Java Sort Array Objects using Comparable Interface.
Check out Comparator example at https://www.javaguides.net/2018/12/java-comparator-interface-example.html
Learn Java Array at https://www.javaguides.net/2020/04/java-array-tutorial-for-beginners.html.
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course