Monday, March 24, 2025

How to sort array in decreasing order in Java? Example Tutorial

It's easy to sort an array on increasing order by using Arrays.sort() method, which by default sort the array on increasing order, but if you have to sort array into decreasing order then how do you it? For example, if you have an array of Strings {"a", "b", "c"} how do you sort them into descending order? Well, if you remember, Arrays.sort() is overloaded and there is another version, which accepts a Comparator. To sort the array in decreasing order, you can simply providing a comparator which compares element in the reverse order. Thankfully there is one such Comparator already exists in JDK API, Collections.reverseOrder()

If you remember, we have used this Comparator while sorting ArrayList in decreasing order, You can use it to sort array into decreasing order as well. This is all good if you array of Objects which can be compared, but how about sorting an array of primitives into descending order? 

How do you do that? I know what you are saying, why can't we use the same approach? Well, primitive versions of Arrays.sort() is not overloaded to accept Comparator, because it is only meant to compare Objects not primitive. 

So, you have two choices, rollout your own sorting method based upon QuickSort or MergerSort ore leverage the existing one by converting your array to ArrayList and then back. 


Sorting array of objects in decreasing order

Here is the code to sort an array of objects, String here in decreasing order

String[] letters = new String[]{"a", "b", "c", "d", "e"};

Arrays.sort(letters, Collections.reverseOrder());


Sorting primitive array in decreasing order 

Now, let's sort a primitive array in decreasing order, for example int array

int[] numbers = new int[]{1, 2, 3, 4, 5};

Arrays.sort(numbers, Collections.reverseOrder());


Java Program to sort array in descending order

Here is the complete Java program which you can copy and execute in your favorite IDE like Eclipse or IntelliJ IDEA or any online Java compiler like JDoodle. 

import java.util.Arrays;
import java.util.Collections;


/**
 * Thread Safe Singleton in Java using Double checked locking.
 * @author WINDOWS 8
 *
 */
public class ReverseArrayInPlace {

    
    public static void main(String args[]){
        
        String[] letters = new String[]{"a", "b", "c", "d", "e"};
        
        System.out.println("array before sorting : " + Arrays.toString(letters));
        
        // sorting array in decreasing order
        Arrays.sort(letters, Collections.reverseOrder());
        
        
    }

}

Output
array before sorting : [a, b, c, d, e]
array after sorting : [e, d, c, b, a]


Summary

And, here is a nice summary image to revise and remember different ways to sort an array in decreasing order in Java. 

How to sort array in decreasing order in Java? Example Tutorial


That's all about how to sort array in decreasing order in Java. You have learned to sort both array of objects and primitives into reverse order. It's better to rollout your own method based upon Java API, but if you nor facing performance challenge then converting array to ArrayList and then back is also not a bad option. 


    Preparing for Java Developer Interviews?

      We respect your privacy. Unsubscribe at any time.

      No comments :

      Post a Comment