Home  >  Java 8

Java List sort() Example

By Arvind Rai, February 07, 2020
The sort is default method of java.util.List interface. The sort method of List has been introduced in Java 8.
1. The sort method accepts the Comparator as an argument and sorts this List according to specified Comparator.
default void sort(Comparator<? super E> c) 
2. If the elements of List are comparable i.e. the element class is implementing Comparable interface, then we can pass null to sort method and sorting will take place according to natural ordering.
3. The List must not be resizable though it can be modifiable.

sort() with Comparator

Example-1: We will create a Comparator object using Comparator.comparing method.
Comparator<Student> nameComparator = Comparator.comparing(Student::getName); 
We pass above object to sort method of the List.
students.sort(nameComparator); 
The list students will sort all the students object according to the order induced by specified Comparator.
We can reverse the soring order as following.
students.sort(nameComparator.reversed()); 
Now find the complete example.
ListSort1.java
package com.concretepage;
import java.util.Comparator;
import java.util.List;
public class ListSort1 {
  public static void main(String[] args) {
	List<Student> students = Student.getStudents();

	System.out.println("--- Sort by name in ascending order ---");
	Comparator<Student> nameComparator = Comparator.comparing(Student::getName);
	students.sort(nameComparator);
	students.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by name in descending order ---");
	students.sort(nameComparator.reversed());
	students.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by age in ascending order ---");
	Comparator<Student> ageComparator = Comparator.comparing(Student::getAge);
	students.sort(ageComparator);
	students.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by age in descending order ---");
	students.sort(ageComparator.reversed());
	students.forEach(s -> System.out.println(s));	
  }
} 
Student.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class Student {
  private String name;
  private int age;
  public Student(String name, int age) {
	this.name = name;
	this.age = age;
  }

  //Setters and Getters

  public static List<Student> getStudents() {
	Student s1 = new Student("Lakshman", 25);
	Student s2 = new Student("Ram", 30);
	Student s3 = new Student("Shatrudhan", 23);
	Student s4 = new Student("Bharat", 28);

	return Arrays.asList(s1, s2, s3, s4);
  }
  @Override
  public String toString() {
	return name + " - " + age;
  }
} 
Output
--- Sort by name in ascending order ---
Bharat - 28
Lakshman - 25
Ram - 30
Shatrudhan - 23
--- Sort by name in descending order ---
Shatrudhan - 23
Ram - 30
Lakshman - 25
Bharat - 28
--- Sort by age in ascending order ---
Shatrudhan - 23
Lakshman - 25
Bharat - 28
Ram - 30
--- Sort by age in descending order ---
Ram - 30
Bharat - 28
Lakshman - 25
Shatrudhan - 23 

Example-2: Here we will create comparator classes by implementing Comparator interfaces and use them with sort method.
ListSort2.java
package com.concretepage;
import java.util.Comparator;
import java.util.List;

class NameComparator implements Comparator<Student> {
  @Override
  public int compare(Student s1, Student s2) {
     return s1.getName().compareTo(s2.getName());
  }
}

class AgeComparator implements Comparator<Student> {
  @Override
  public int compare(Student s1, Student s2) {
     return s1.getAge() - s2.getAge();
  }
}

public class ListSort2 {
  public static void main(String[] args) {
	List<Student> students = Student.getStudents();

	System.out.println("--- Sort by name in ascending order ---");
	students.sort(new NameComparator());
	students.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by age in Ascending order ---");
	students.sort(new AgeComparator());
	students.forEach(s -> System.out.println(s));
	
  }
} 
Output
--- Sort by name in ascending order ---
Bharat - 28
Lakshman - 25
Ram - 30
Shatrudhan - 23
--- Sort by age in Ascending order ---
Shatrudhan - 23
Lakshman - 25
Bharat - 28
Ram - 30 

sort() with Comparable

If the element class of List is implementing Comparable interface, we can sort it using sort method of List i.e. natural ordering. To sort the elements of List according to its natural order using sort method, we can pass null or Comparator.naturalOrder() to sort method.
employees.sort(null);
employees.sort(Comparator.naturalOrder()); 
In both the above cases, sorting of elements will be done according to its natural order. Find the example.
ListSort3.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ListSort3 {
  public static void main(String[] args) {
	Employee e1 = new Employee(101, "Mohit");
	Employee e2 = new Employee(102, "Mahesh");
	Employee e3 = new Employee(103, "Jitendra");
	Employee e4 = new Employee(104, "Keshav");	
	List<Employee> employees = Arrays.asList(e1, e2, e3, e4);
	
	System.out.println("--- Sort by employee name in ascending order ---");
	employees.sort(null);
	//employees.sort(Comparator.naturalOrder());
	employees.forEach(s -> System.out.println(s));
	
	System.out.println("--- Sort by employee name in descending order ---");
	employees.sort(Comparator.reverseOrder());
	employees.forEach(s -> System.out.println(s));	
  }
}

class Employee implements Comparable<Employee> {
  private int empId;
  private String empName;
  public Employee(int empId, String empName) {
	this.empId = empId;
	this.empName = empName;
  }
  //Setters and Getters
  
  @Override
  public int compareTo(Employee e) {
	return empName.compareTo(e.getEmpName());
  }
  @Override  
  public String toString(){
	return empId + "-" + empName; 
  }  
} 
Output
--- Sort by employee name in ascending order ---
103-Jitendra
104-Keshav
102-Mahesh
101-Mohit
--- Sort by employee name in descending order ---
101-Mohit
102-Mahesh
104-Keshav
103-Jitendra 

Reference

Java doc: List
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2020 concretepage.com | Privacy Policy | Contact Us