Java sort()

The sort() method of the collections framework uses the merge sort algorithm to sort elements of a collection.

The merge sort algorithm is based on divide and conquers rule. To learn more about the merge sort, visit Merge Sort Algorithm.

Let's take an example of the sort() method.


Example: Sorting in Ascending Order

import java.util.ArrayList;
import java.util.Collections;

class Main {
    public static void main(String[] args) {

        // Creating an array list
        ArrayList<Integer> numbers = new ArrayList<>();

        // Add elements
        numbers.add(4);
        numbers.add(2);
        numbers.add(3);
        System.out.println("Unsorted ArrayList: " + numbers);

        // Using the sort() method
        Collections.sort(numbers);
        System.out.println("Sorted ArrayList: " + numbers);

    }
}

Output

Unsorted ArrayList: [4, 2, 3]
Sorted ArrayList: [2, 3, 4]

As you can see, by default, the sorting occurs in natural order (ascending order). However, we can customize the sorting order of the sort() method.


Customized Sorting Order

In Java, the sort() method can be customized to perform sorting in reverse order using the Comparator interface.


Example: Sorting in Descending Order

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class Main {
    public static void main(String[] args) {

        // Creating an array list
        ArrayList<Integer> numbers = new ArrayList<>();

        // Add elements
        numbers.add(4);
        numbers.add(2);
        numbers.add(3);
        System.out.println("Unsorted ArrayList: " + numbers);

        // Using the sort() method
        Collections.sort(numbers);
        System.out.println("Natural Sorting: " + numbers);

        // Using the customized sort() method
        Collections.sort(numbers, new CustomComparator());
        System.out.println("Customized Sorting: " + numbers);

    }
}

class CustomComparator implements Comparator<Integer> {

    @Override
    public int compare(Integer animal1, Integer animal2) {
        int value =  animal1.compareTo(animal2);
        // elements are sorted in reverse order
        if (value > 0) {
            return -1;
        }
        else if (value < 0) {
            return 1;
        }
        else {
            return 0;
        }
    }
}

Output

Unsorted ArrayList: [4, 2, 3]
Natural Sorting: [2, 3, 4]
Customized Sorting: [4, 3, 2]

In the above example, we have used the sort() method with CustomComparator as an argument.

Here, CustomComparator is a class that implements the Comparator interface. Learn more about the Java Comparator Interface.

We then override the compare() method. The method will now sort elements in reverse order.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges