How to sort a List/ArrayList?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Sorting a List or ArrayList is a common operation in software development, allowing data to be organized in a specific order. In programming languages like Java, a List is an ordered collection (also known as a sequence). The List interface provides a way to store the ordered collection. An ArrayList is a resizable-array implementation of the List interface. Below, we explore different methods and techniques for sorting these collections, particularly focusing on Java.
Sorting using Collections.sort()
In Java, the Collections.sort() method is one of the most common ways to sort an ArrayList. This method sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. The elements' natural ordering must be consistent with equals if it is to correctly sort a list.
Example:
In the example, fruits is an ArrayList that gets sorted alphabetically.
Sorting with a Custom Comparator
You may want to sort objects in a way that does not follow their natural order. For this, Java offers the Comparator interface, which you can pass as the second argument to the Collections.sort() method.
Example:
In this example, employees are sorted by their name using a custom Comparator.
Using Java 8 Stream API
Java 8 introduced the Stream API, which provides a more declarative approach to handling collections, including sorting.
Example:
Here, sorted() sorts the elements according to their natural order and the result is collected back into a list.
Summary Table
| Method | Description | Suitable for |
Collections.sort() | Sorts based on natural ordering or using a custom comparator. | Simple types and when custom comparator is needed. |
Java 8 Stream API sorted() | Provides a declarative way to sort collections using stream operations. | Modern Java environments, when working with streams. |
Additional Techniques and Considerations
- Stability: When sorting with custom comparators, consider whether the sort needs to be stable (order of equal elements is preserved). Java's sort is stable by nature.
- Performance: Always consider the performance implications of sorting, especially with large lists. The sort operation usually has a time complexity of .
- Null Elements: If the list contains
nullvalues, ensure that the comparator can handle nulls, or use methods likeComparator.nullsFirst()orComparator.nullsLast().
By understanding these different methods and considerations, you can use Java's powerful capabilities to sort lists and ArrayLists effectively and efficiently.
Related reading
- How to Sort a List<T> by a property in the object
- How to sort a list/tuple of lists/tuples by the element at a given index
- How to sort a m x n matrix which has all its m rows sorted and n columns sorted?
- How to sort a pandas dataFrame by two or more columns?
- How to sort a stack using only stack operations?
- How to sort a string list consists of digits and alphabets in Java?
- How to sort an ArrayList in Java
- How to sort by two fields in Java?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.