W3docs

Why is there no SortedList in Java?

There is no SortedList class in the Java standard library because the List interface already provides a way to store elements in a specific order.

There is no SortedList class in the Java standard library because the List interface is designed to maintain elements in an index-based order (typically insertion order), not a value-based sorted order. If you need a collection that automatically keeps elements sorted by their natural ordering or a custom comparator, Java provides the SortedSet interface and its TreeSet implementation instead.

For example, you can use the following code to create a list that preserves insertion order:


import java.util.ArrayList;
import java.util.List;

List<Integer> list = new ArrayList<>();

// add elements to the list in the desired order
list.add(1);
list.add(2);
list.add(3);

// the list is now [1, 2, 3]

Alternatively, you can sort an existing list in ascending order using the modern List.sort() method:


import java.util.ArrayList;
import java.util.List;

List<Integer> list = new ArrayList<>();

list.add(3);
list.add(1);
list.add(2);

// sort the list in ascending order
list.sort(null); // uses natural ordering

// the list is now [1, 2, 3]

Keep in mind that the List interface guarantees order by index (position), not by value. You should explicitly sort the list when needed, or use a SortedSet/TreeSet if you require elements to remain sorted automatically.