Sort a List of strings in Java
This post will discuss how to sort a list of strings in lexicographical order in Java.
1. Using Collections.sort() method
A simple solution to in-place sort a list of strings in lexicographical order is using the Collections.sort() method. It takes a modifiable list, which need not be resizable.
|
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; class Main { public static void main(String[] args) { List<String> faang = Arrays.asList("Facebook", "Amazon", "Apple", "Netflix", "Google"); Collections.sort(faang); System.out.println(faang); } } |
Output:
[Amazon, Apple, Facebook, Google, Netflix]
The Collections.sort() method optionally takes a comparator to allow precise control over the sort order. To make the comparison between two strings case-insensitive, you can use the String.CASE_INSENSITIVE_ORDER comparator.
|
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; class Main { public static void main(String[] args) { List<String> faang = Arrays.asList("Facebook", "Amazon", "Apple", "Netflix", "Google"); Collections.sort(faang, String.CASE_INSENSITIVE_ORDER); System.out.println(faang); } } |
Output:
[Amazon, APPLE, Facebook, GOOGLE, Netflix]
2. Using List.sort() method
Another alternative to in-place sort a list of strings is with the List.sort() method, which was added to the specification in JDK 1.8. The Collections.sort() method is a wrapper over the List.sort() method. Hence, the above code is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> faang = Arrays.asList("Facebook", "Amazon", "Apple", "Netflix", "Google"); faang.sort(null); System.out.println(faang); } } |
Output:
[Amazon, Apple, Facebook, Google, Netflix]
You can use the String.CASE_INSENSITIVE_ORDER comparator to make the sort operation compare strings by ignoring their order.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> faang = Arrays.asList("Facebook", "Amazon", "Apple", "Netflix", "Google"); faang.sort(String.CASE_INSENSITIVE_ORDER); System.out.println(faang); } } |
Output:
[Amazon, APPLE, Facebook, GOOGLE, Netflix]
3. Using Stream.sorted() method
To create a sorted copy of the list, you can use Java 8 Stream. The idea is to create a sequential Stream over the elements in the list, sort the stream using the sorted() method, and collect all the sorted elements into a new List. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<String> faang = Arrays.asList("Facebook", "Amazon", "Apple", "Netflix", "Google"); List<String> sorted_list = faang.stream().sorted().collect(Collectors.toList()); System.out.println(sorted_list); } } |
Output:
[Amazon, Apple, Facebook, Google, Netflix]
4. Using Guava
If you happen to use the Guava library in your project, you may want to explore the Ordering class. It offers the sortedCopy() method that returns a mutable list containing elements sorted by this ordering.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.Ordering; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> faang = Arrays.asList("Facebook", "Amazon", "Apple", "Netflix", "Google"); List<String> sorted_list = Ordering.natural().sortedCopy(faang); System.out.println(sorted_list); } } |
Output:
[Amazon, Apple, Facebook, Google, Netflix]
5. Using TreeSet
In a TreeSet, elements are ordered using their natural ordering or by provided Comparator. If your list contains distinct elements, you can insert all its elements into a TreeSet to get a sorted collection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.TreeSet; class Main { public static void main(String[] args) { List<String> faang = Arrays.asList("Facebook", "Amazon", "Apple", "Netflix", "Google"); List<String> sorted_list = new ArrayList<>(new TreeSet<>(faang)); System.out.println(sorted_list); } } |
Output:
[Amazon, Apple, Facebook, Google, Netflix]
That’s all about sorting a List of strings 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 :)