What is the best way to sort a List/ArrayList in Java?
I have a List of doubles and I need to sort it in descending order. I am using an ArrayList and my input data looks like this:
List<Double> testList = new ArrayList<>();
testList.add(0.5);
testList.add(0.2);
testList.add(0.9);
testList.add(0.1);
testList.add(0.1);
testList.add(0.1);
testList.add(0.54);
testList.add(0.71);
testList.add(0.71);
testList.add(0.71);
testList.add(0.92);
testList.add(0.12);
testList.add(0.65);
testList.add(0.34);
testList.add(0.62);
The expected output should be sorted in descending order, like this:
0.92
0.9
0.71
0.71
0.71
0.65
0.62
0.54
0.5
0.34
0.2
0.12
0.1
0.1
0.1
How can I achieve this efficiently? What are the best ways to perform Java sort list operations while keeping performance in mind?
Hey! From my experience, if you’re looking to sort a list in Java in descending order, a great way to do it is using Collections.sort()
along with a custom Comparator
. Here’s a simple example for your case:
import java.util.*;
public class SortListExample {
public static void main(String[] args) {
List<Double> testList = new ArrayList<>(Arrays.asList(0.5, 0.2, 0.9, 0.1, 0.1, 0.1,
0.54, 0.71, 0.71, 0.71, 0.92,
0.12, 0.65, 0.34, 0.62));
// Sorting in descending order
Collections.sort(testList, (a, b) -> Double.compare(b, a));
// Printing the sorted list
System.out.println(testList);
}
}
In this example, Collections.sort()
is used with a lambda expression to sort the list in descending order. It’s efficient and works well for smaller lists, but performance might not be as optimized for very large datasets.
that works great for small lists! But if you’re using Java 8 or later, there’s a more streamlined way to do the same thing with List.sort()
—which is much more elegant and concise:
testList.sort(Comparator.reverseOrder());
It’s essentially the same as Collections.sort()
, but it’s cleaner, as you don’t have to pass in a comparator explicitly. The code looks more readable and modern, and I’d say this is probably the go-to approach if you’re working with any recent Java version. It’s really easy to implement, and the performance is on par with Collections.sort()
. So, with java sort list
, this should give you a clean and efficient solution.
Nice! But if you’re into Java Streams, here’s an even more functional approach. You can use Streams to sort the list in descending order like this:
List<Double> sortedList = testList.stream()
.sorted(Comparator.reverseOrder())
.toList(); // toList() is available from Java 16+
System.out.println(sortedList);
This method is particularly helpful if you’re already working with Streams in your code. It’s not just about sorting but also chaining other operations in a fluent, readable way. Performance-wise, for large lists, Streams can sometimes have a slight overhead due to internal operations, but it’s still a highly efficient way of working with collections, especially if you’re familiar with functional programming. So, java sort list
via Streams might be your best bet if you’re into that functional style!