There are many ways to sort a list in Java. In this tutorial, we will cover sorting using the built-in methods.
If we work with numbers, we can sort a List in Java using:
- Collections.sort(List<T> list) method
- ArrayList.sort(Comparator<? super E> c) method
- Java 8 Stream – sorted() operation
If we work with custom objects, we can sort a list using the following ways:
- By implementing the Comparable interface
- Using the Collections.sort(List<T> list, Comparator<? super T> c) method
Sorting a list of numbers in Java
Using the sort() method from Collections class
This method only works with objects that implement the Comparable interface. Here, we will sort the list of Integers.
import java.util.*; public class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 5, 28, 97, 14, 2, 7, 8, 12, 19, 22)); Collections.sort(numbers); System.out.println(numbers); } }
Using the sort() method from ArrayList class
This method accepts Comparator. We can sort a list of integers by calling Comparator.naturalOrder() method and passing it as a parameter to the sort() method.
public class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 5, 28, 97, 14, 2, 7, 8, 12, 19, 22)); numbers.sort(Comparator.naturalOrder()); System.out.println(numbers); } }
Sort a List in Java using the Java Streams sorted() operation
We can use the Streams API introduced as part of Java 8. There is a sorted() method that we can use to sort the elements of a Stream, and then we need to call the Collectors.toList() to return the sorted list.
import java.util.*; import java.util.stream.Collectors; public class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 5, 28, 97, 14, 2, 7, 8, 12, 19, 22)); List<Integer> sortedList = numbers.stream().sorted().collect(Collectors.toList()); System.out.println(sortedList); } }
Sorting a list of custom objects in Java
Sorting custom objects by implementing a Comparable interface
If we want to sort objects of the custom class, it needs to implement a Comparable interface.
We will sort a list of users based on a userId field.
import java.util.ArrayList; import java.util.Collections; import java.util.List; class User implements Comparable<User> { public int userId; public String username; public String address; public User(int userId, String username, String address) { this.userId = userId; this.username = username; this.address = address; } @Override public int compareTo(User user) { if (this.userId == user.userId) { return 0; } else if (this.userId > user.userId) { return 1; } else { return -1; } } } class Test { public static void main(String[] args) { List<User> users = new ArrayList<>(); users.add(new User(5, "username1", "Address1")); users.add(new User(2, "username2", "Address2")); users.add(new User(7, "username3", "Address3")); users.add(new User(3, "username4", "Address4")); Collections.sort(users); // it will sort the users based on the userId field for (User user : users) { System.out.println(user.userId + " " + user.username + " " + user.address); } } }
Sorting custom objects using the Collections.sort(List<T> list, Comparator<? super T> c) method
Suppose we need to sort objects that don’t implement the Comparable interface. In that case, we can do that by specifying the Comparator and passing it as a second parameter to the sort() method.
We will sort a list of students based on a grade field in this example.
public class Test { public static void main(String[] args) { List<Student> students = new ArrayList<>(); students.add(new Student("Megan", 7.5, 2)); students.add(new Student("Tom", 8.2, 3)); students.add(new Student("Suzan", 7.2, 1)); students.add(new Student("Steve", 9.1, 3)); Collections.sort(students, Comparator.comparing(Student::getGrade)); System.out.println(students); } } class Student { private String name; private double grade; private int year; public Student(String name, double grade, int year) { this.name = name; this.grade = grade; this.year = year; } // Getters, setters and toString method }