Sort an array of strings in Java
This article will discuss various methods to sort an array of strings in Java.
1. Sort array of strings using Arrays.sort()
method
We can use Arrays
class that has several static overloaded methods for sorting:
⮚ Arrays.sort(String[])
It sorts the specified array of strings into ascending order, according to the natural ordering of its elements. It uses dual-pivot Quicksort, which is typically faster than traditional single-pivot Quicksort.
1 2 3 |
String[] s = { "sort", "string", "array" }; Arrays.sort(s); System.out.println(Arrays.toString(s)); // [array, sort, string] |
⮚ Arrays.sort(String[], Comparator)
It sorts the specified array of strings according to the order induced by the specified comparator. It requires all array elements to be mutually comparable by the specified comparator, i.e., for any pair of strings (s1, s2)
in the array, c.compare(s1, s2)
should not throw a ClassCastException
. This sort uses Iterative Merge Sort and produces a stable sort, meaning that it will preserve the relative order of equal strings.
⮚ To sort in ascending order:
1 2 3 |
String[] s = { "sort", "string", "array" }; Arrays.sort(s, Comparator.naturalOrder()); System.out.println(Arrays.toString(s)); // [array, sort, string] |
⮚ To sort in descending order:
1 2 3 |
String[] s = { "sort", "string", "array" }; Arrays.sort(s, Comparator.reverseOrder()); // or, use `Collections.reverseOrder()` System.out.println(Arrays.toString(s)); // [string, sort, array] |
We can also write our custom comparator, as shown below:
1 2 3 4 5 6 7 8 |
String[] s = { "sort", "string", "array" }; Arrays.sort(s, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); // sort in descending order } }); System.out.println(Arrays.toString(s)); // [string, sort, array] |
2. Sort array of strings using Arrays.parallelSort()
method
The Arrays.sort()
method uses only a single thread to sort the elements. Java 8 provides Arrays.parallelSort()
which uses multiple threads for sorting and beats Arrays.sort()
when number of elements cross a certain threshold. The prototype of the parallelSort()
is similar to sort()
.
1 2 3 |
String[] s = { "sort", "string", "array" }; Arrays.parallelSort(s); System.out.println(Arrays.toString(s)); // [array, sort, string] |
3. Sort array of strings using Java 8
We can also use Java 8 Stream to sort an array of strings. The idea is to get a sequential stream of strings from the specified string array and sort it according to natural order or reverse order using a comparator. Finally, we convert the sorted stream back to the string array.
⮚ To sort array of strings in ascending order:
1 2 3 |
String[] s = { "sort", "string", "array" }; s = Arrays.stream(s).sorted().toArray(String[]::new); // or, use `Stream.of()` System.out.println(Arrays.toString(s)); // [array, sort, string] |
⮚ To sort array of strings in descending order:
1 2 3 4 |
String[] s = { "sort", "string", "array" }; s = Arrays.stream(s).sorted(Collections.reverseOrder()) .toArray(String[]::new); System.out.println(Arrays.toString(s)); // [string, sort, array] |
4. Sort array of strings using Collections.sort()
method
We know that the Arrays.asList()
method returns a fixed-size list backed by the specified array. That means any changes in the original array will be reflected in the returned list. We can make use of this fact to sort a list returned by Arrays.asList()
using the Collections.sort()
method, which in turn sorts the original array.
1 2 3 |
String[] s = { "sort", "string", "array" }; Collections.sort(Arrays.asList(s)); System.out.println(Arrays.toString(s)); // [array, sort, string] |
That’s all about sorting an array of strings in Java.
Reference: Arrays (Java Platform SE 21)
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 :)