Lewis D.
—You’ve created a List
object in your Java project, but some functionality requires the values to be sorted in a specific order. How can you do this?
There are several solutions to this problem. Let’s take a look at some of the more common ones.
Collections
ClassThe java.utils.Collections
library provides the method sort(List<T>)
, which will take an implemented List
object and sort it based on the natural order of the value type:
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Example { public static void main(String[] args) { List<Integer> test = new ArrayList<>(Arrays.asList(8, 6, 3, 7, 2)); Collections.sort(test); for (Integer i : test) { System.out.print(i); } } }
Output:
23678
If you want to change the ordering of your sort, use the overloaded sort(List<T> list, Comparator<? super T> c)
method and provide a comparator:
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Example { public static void main(String[] args) { List<Integer> test = new ArrayList<>(Arrays.asList(8, 6, 3, 7, 2)); Collections.sort(test, Comparator.reverseOrder()); for (Integer i : test) { System.out.print(i); } } }
Output:
87632
List.sort()
The comparator interface provides the framework to compare different comparable objects.
The List.sort(Comparator<? super T> c)
method takes a comparator as an argument, and will return a sorted array based on the comparator order specified:
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Example { public static void main(String[] args) { List<Integer> test = new ArrayList<>(Arrays.asList(8, 6, 3, 7, 2)); test.sort(Comparator.naturalOrder()); for (Integer i : test) { System.out.print(i); } } }
Output:
23678
This method in the List
class is practically identical to the Collections
class implementation, and so the order can be changed with whatever comparator it receives as an argument.
Stream
ObjectYou can also use the List.stream()
method to sort your list. This method will return a sequential Stream
object containing the values from the list. You can then use the Stream.sorted(Comparator<? super T> c)
method to order your list based on a provided comparator. You will then need to convert the Stream
object back into a List
.
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { List<Integer> test = new ArrayList<>(Arrays.asList(8, 6, 3, 7, 2)); test = test.stream() .sorted(Comparator.naturalOrder()) .collect(Collectors.toList()); for (Integer i : test) { System.out.print(i); } } }
Output:
23678
These methods all behave in a similar way, and they are mostly interchangeable.
All three implement a version of a merge sort and have an average time complexity of O(n*Log n)
.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.