Friday, July 2, 2021

How to Sort a List into Ascending and Descending Order in Java? Examples

ArrayList, Set Sorting in Ascending – Descending Order Java
Sorting List, Set, and ArrayList in Java in ascending and descending order is very easy, You just need to know the correct API method to do that. Collections.sort()  method will sort the collection passed to it,  doesn't return anything just sort the collection itself.  Sort() method of Collections class in Java is overloaded where another version takes a Comparator and sorts all the elements of Collection on order defined by Comparator

If we don't pass any Comparator then objects will be sorted based upon their natural order like String will be sorted alphabetically or lexicographically. Integer will be sorted numerically etc. 

The default sorting order for an object is ascending order like Integer will be sorted from low to high while descending order is just opposite. Collections.reverseOrder() returns a Comparator which will be used for sorting Objects in descending order.


Sorting List in ascending and descending order - Example

Sort List in ascending and descending order in Java exampleHere full code example of how to sort List in Java in both ascending and descending order in Java. It's not difficult just to remember that Collections.sort() is a key method that sorts objects of Collection. You can pass it Comparator for defining sorting order. If no comparator passed then Object must implement a Comparable interface in order to be sorted.


package example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Java program Example of sorting List, ArrayList and Set on ascending and descending order
 * in Java.
 * Collections.sort() method is used for sorting collection and Collections.reverseOrder()
 * will sort elements of Collections in descending order.
 * @author Javin Paul
 */

public class CollectionSortingExample{
   
    public static void main(String args[]) throws InterruptedException{            
   
        // Sort List and Set in ascending order
       
        // Sorting List in Java in ascending order in Java
        List<Integer> list = Arrays.asList( 1, 2, 5, 9, 3, 6, 4, 7, 8);
       
        System.out.println("Unsorted List in Java: " + list);
       
        // Sorting List into Java now, Collections.sort()  method will sort
        // the collection passed
        // to it. Doesn't return anything it just sorts the collection itself

        Collections.sort(list); //sorting collection
       
        System.out.println("List in Java sorted in ascending order: " + list);
       
        // sorting List in descending order in Java, Collections.sort() method can be used
        // to sort all element in descending order if we pass it comparator which can
        // return descending order for elements. here is an example of sorting List
        // in descending order  in Java
        // Collection class provides a in built-in comparator for that which can
        // sort objects in reverse order i..e descending order for Integers or
        // any other Object in Java
       
        Collections.sort(list, Collections.reverseOrder());
        System.out.println("Java List sorted in descending order: " + list);
       
        // Any List implementation in Java like ArrayList, LinkedList
        // can be sorted in ascending and descending order in Java by following above
        // example, let's see a quick example for sorting ArrayList and LinkedList
        // in ascending and descending order in Java
       
       
        //Sorting ArrayList in ascending order in Java
        ArrayList alphabets = new ArrayList();
        alphabets.add("c");
        alphabets.add("b");
        alphabets.add("a");
        alphabets.add("d");
       
        System.out.println("Unsorted ArrayList in Java : " + alphabets);
       
        //Sorting ArrayList into ascending order
        Collections.sort(alphabets);
       
        System.out.println("Sorted ArrayList in ascending order in Java : " + alphabets);
       
        //Sorting ArrayList into descending order or reverse order in Java
        Collections.sort(alphabets, Collections.reverseOrder());
       
        System.out.println("ArrayList sort in descending order in Java : " + alphabets);      
       
    }  
 
}


Output:
Unsorted List in Java: [1, 2, 5, 9, 3, 6, 4, 7, 8]
List in Java sorted in ascending order: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Java List sorted in descending order: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Unsorted ArrayList in Java : [c, b, a, d]
Sorted ArrayList in ascending order in Java : [a, b, c, d]
ArrayList sort in descending order in Java : [d, c, b, a]


As shown in this sort example of ArrayList and Set, You can sort LinkedList in ascending and descending order in Java. Since the Collections.sort() method is applicable to the Collection interface, any Collection implementation like Set, HashSet can also be sorted in ascending, descending, or reverse order of elements. 

Further Learning
What is the difference between ArrayList and Vector in Java?
How to iterate over ArrayList in Java
How to loop Map in Java with Example
Difference between ConcurrentHashMap and Hashtable in Java
Difference between Hashtable and HashMap in Java

By the way, Sorting arrays in Java is completely different than sorting collection in Java which we will see in the next tutorial. Now you learned how to sort Collection in Java including frequently used collections like ArrayList, List, Set, and HashSet.

3 comments:

Feel free to comment, ask questions if you have any doubt.