Sort List in ascending order in Java
This post will discuss several ways to sort the list in ascending order in Java. Assume the specified list is modifiable but not necessarily resizable.
1. Using Collections.sort() method
Collections utility class provides a static sort() method for sorting the specified list into ascending order, according to the natural ordering of its elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.Collections; import java.util.List; // Sort list in ascending order in Java class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); Collections.sort(list); System.out.println(list); } } |
Output:
[2, 4, 5, 6, 8, 10]
This method will produce a stable sort. This will work only if all the list elements implement the Comparable interface and are mutually comparable, i.e., for any pair of elements (a, b) in the list, a.compareTo(b) does not throw a ClassCastException.
2. Using List.sort() method
Every List implementation provides a static sort() method that sorts the list according to the order induced by the specified Comparator. For this method to work, all the list elements must be mutually comparable using the specified comparator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.Comparator; import java.util.List; // Sort list in ascending order in Java class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); list.sort(Comparator.naturalOrder()); System.out.println(list); } } |
Output:
[2, 4, 5, 6, 8, 10]
If the specified comparator is null, then all elements in this list must implement the Comparable interface, and the element’s natural ordering will be used.
|
1 2 3 |
List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); list.sort(null); System.out.println(list); // [2, 4, 5, 6, 8, 10] |
3. Using Java 8
Sorting a List became even easier with an introduction of Stream in Java 8 and above. The idea is to get a stream consisting of the elements of the list, sort it in natural order using the Stream.sorted() method and finally collect all sorted elements in a list using Stream.collect() with Collectors.toList(). For sorting to work, all elements of the list should be mutually Comparable.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; // Sort list in ascending order in Java class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); list = list.stream() .sorted() .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[2, 4, 5, 6, 8, 10]
4. Sort list of objects
Collections.sort(list) and list.sort(null) method will work only if all elements of the list implements the Comparable interface.
For example, the following code creates a list of User and since User class doesn’t implement Comparable, the program will throw a Compilation error on calling Collections.sort() and a ClassCastException on List.sort(null).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Arrays; import java.util.Collections; import java.util.List; class User { private String name; public User(String name) { this.name = name; } @Override public String toString() { return name; } } class Main { public static void main(String[] args) { List<User> users = Arrays.asList(new User("John"), new User("Smith"), new User("Andrew")); Collections.sort(users); // Compilation error System.out.println(users); } } |
There are several ways to sort a list of Objects:
1. Make the Object implement the Comparable interface and override the compareTo() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; class Company implements Comparable<Company> { private String company; private int rank; public Company(String company, int rank) { this.company = company; this.rank = rank; } @Override public int compareTo(Company o) { return company.compareTo(o.company); } @Override public String toString() { return "{" + company + ", " + rank + "}"; } } // Sort list in ascending order in Java class Main { public static void main(String[] args) { List<Company> list = Arrays.asList(new Company("Google", 1), new Company("Apple", 3), new Company("Microsoft", 2)); // sort list based on the natural order of object Collections.sort(list); System.out.println(list); } } |
Output:
[{Apple, 3}, {Google, 1}, {Microsoft, 2}]
2. Pass a Comparator to the Collections.sort() method, which defines how sorting of objects will occur in the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; class Company { private String company; private int rank; public Company(String company, int rank) { this.company = company; this.rank = rank; } @Override public String toString() { return "{" + company + ", " + rank + "}"; } public int getPrice() { return rank; } } // Sort list in ascending order in Java class Main { public static void main(String[] args) { List<Company> list = Arrays.asList(new Company("Google", 1), new Company("Apple", 3), new Company("Microsoft", 2)); // sort list based on custom order defined by Comparator Collections.sort(list, new Comparator<Company>() { @Override public int compare(Company o1, Company o2) { return o1.getPrice() - o2.getPrice(); } }); System.out.println(list); } } |
Output:
[{Google, 1}, {Microsoft, 2}, {Apple, 3}]
3. Pass lambda expression to Collections.sort() method that defines list sorting order. This will work only on Java 8 and above.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Arrays; import java.util.Collections; import java.util.List; class Company { private String company; private int rank; public Company(String company, int rank) { this.company = company; this.rank = rank; } @Override public String toString() { return "{" + company + ", " + rank + "}"; } public int getPrice() { return rank; } } // Sort list in ascending order in Java 8 and above class Main { public static void main(String[] args) { List<Company> list = Arrays.asList(new Company("Google", 1), new Company("Apple", 3), new Company("Microsoft", 2) ); // define custom order to sort a list using lambda expression Collections.sort(list, (o1, o2) -> o1.getPrice() - o2.getPrice()); System.out.println(list); } } |
Output:
[{Google, 1}, {Microsoft, 2}, {Apple, 3}]
That’s all about sorting a list in ascending order in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)