How to Sort List into Ascending and Descending Order in Java

ArrayList, Set Sorting in Ascending – Descending Order Java
Sorting List, Set and ArrayList in Java on ascending and descending order is very easy, You just need to know 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 sort all the elements of Collection on order defined by Comparator.If we  don't pass any Comparator than object will be sorted based upon there natural order like String will be sorted alphabetically or lexicographically. Integer will be sorted numerically etc. 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 Object 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 on both ascending and descending order in Java. Its not difficult just remember that Collections.sort() is a key method which sort objects of Collection. You can pass it Comparator for defining sorting order. If no comparator passed than Object must implement 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 sort 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 on ascending and descending order in Java. Since Collections.sort() method is applicable to Collection interface, any Collection implementation like Set, HashSet can also be sorted in ascending, descending or reverse order of elements. Sorting Array in Java is completely different than sorting collection in Java which we will see in next tutorial. Now you learned how to sort Collection in Java including frequently used collection like ArrayList, List , Set and HashSet.


Related Java tutorial from Collection Framework :
What is 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

1 comment: