How to Sort a List in Java

Sorting is a fundamental operation in computer science. It is the process of arranging elements in a specific order, such as ascending or descending.

Sorting is important because it helps to organize data, making it easier to search, analyze and interpret.

In Java, there are several ways to sort a list of elements, and in this article, we will explore the most commonly used methods.


Using the Collections.sort() Method

The Collections.sort() method is a built-in Java method that can be used to sort a list of elements.

This method is available in the java.util package and is applicable to lists that implement the List interface.

To use the Collections.sort() method, you simply pass the list you want to sort as an argument. For example, to sort a list of integers, you would write:

List<Integer> numbers = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3);
Collections.sort(numbers);

The above code sorts the numbers list in ascending order.

The Collections.sort() method can also be used to sort a list of objects, but in this case, you need to provide a custom Comparator that defines the sorting criteria.

For example, to sort a list of Person objects by last name, you would write:

List<Person> people = Arrays.asList(new Person("John", "Doe"), new Person("Jane", "Smith"), new Person("Bob", "Johnson"));
Collections.sort(people, new LastNameComparator());

Using the Arrays.sort() Method

The Arrays.sort() method is another built-in Java method that can be used to sort an array of elements.

This method is available in the java.util package and is applicable to arrays of all types, including primitive types and objects.

To use the Arrays.sort() method, you simply pass the array you want to sort as an argument. For example, to sort an array of strings, you would write:

String[] words = {"apple", "banana", "cherry", "date"};
Arrays.sort(words);

The above code sorts the words array in ascending order.

The Arrays.sort() method also has a version that accepts a range and a custom Comparator.

This can be useful if you only want to sort a portion of the array. For example, to sort the last three elements of an array of integers, you would write:

int[] numbers = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
Arrays.sort(numbers, 7, 10);

Using the List.sort() Method (Java 8+)

The List.sort() method is a new method that was added in Java 8. This method can be used to sort a list of elements and it is applicable to lists that implement the List interface.

To use the List.sort() method, you simply call the method on the list you want to sort. For example, to sort a list of custom objects, you would write:

List<Person> people = Arrays.asList(new Person("John", "Doe"), new Person("Jane", "Smith"), new Person("Bob", "Johnson"));
people.sort(Comparator.comparing(Person::getLastName));

by last name using the Comparator.comparing() method. The List.sort() method also has a version that accepts a custom Comparator, which allows you to define your own sorting criteria.

Using a Custom Sorting Algorithm

In some cases, you may want to use a custom sorting algorithm to sort a list of elements. This can be more efficient in certain situations, such as when the list is already partially sorted, or when the list is very small.

A popular custom sorting algorithm is the bubble sort algorithm.

To implement a bubble sort algorithm for a list of integers, you would write:

List<Integer> numbers = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3);
for (int i = 0; i < numbers.size() - 1; i++) {
    for (int j = 0; j < numbers.size() - i - 1; j++) {
        if (numbers.get(j) > numbers.get(j + 1)) {
            int temp = numbers.get(j);
            numbers.set(j, numbers.get(j + 1));
            numbers.set(j + 1, temp);
        }
    }
}

The above code uses nested loops to iterate through the list and compare adjacent elements. If an element is greater than its neighbor, it is swapped. This process is repeated until the list is sorted.


Conclusion

In this article, we have explored the different ways to sort a list in Java, including using the Collections.sort() method, the Arrays.sort() method, the List.sort() method and a custom sorting algorithm.

The best sorting method depends on the specific use case, and it is important to consider factors such as performance and ease of use when making a decision.

Editorial Team
Editorial Team

Programming Cube website is a resource for you to find the best tutorials and articles on programming and coding.