How to Sort List into Ascending in addition to Descending Order inward Java

ArrayList, Set Sorting inwards Ascending – Descending Order Java
Sorting List, Set as well as ArrayList inwards Java on ascending as well as descending social club is rattling easy, You only postulate to know right API method to produce that. Collections.sort()  method volition kind the collection passed to it,  doesn't render anything only kind the collection itself.  Sort() method of Collections shape inwards Java is overloaded where around other version takes a Comparator as well as kind all the elements of Collection on social club defined yesteryear Comparator.If we  don't transcend whatever Comparator than object volition live on sorted based upon at that spot natural order similar String volition live on sorted alphabetically or lexicographically. Integer volition live on sorted numerically etc. Default sorting social club for an object is ascending social club similar Integer volition live on sorted  from depression to high land descending social club is only opposite. Collections.reverseOrder() returns a Comparator which volition live on used for sorting Object inwards descending order.


Sorting List inwards ascending as well as descending social club - Example

 Set as well as ArrayList inwards Java on ascending as well as descending social club is rattling tardily How to Sort List into Ascending as well as Descending Order inwards JavaHere amount code instance of how to kind List inwards Java on both ascending as well as descending social club inwards Java. Its non hard only holler back that Collections.sort() is a cardinal method which kind objects of Collection. You tin transcend it Comparator for defining sorting order. If no comparator passed than Object must implement Comparable interface inwards social club to live on sorted.


package example;

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

/**
 * Java plan Example of sorting List, ArrayList as well as Set on ascending as well as descending order
 * inwards Java.
 * Collections.sort() method is used for sorting collection as well as Collections.reverseOrder()
 * volition kind elements of Collections inwards descending order.
 * @author Javin Paul
 */

public class CollectionSortingExample{
   
    public static void main(String args[]) throws InterruptedException{            
   
        // Sort List as well as Set inwards ascending social club
       
        // Sorting List inwards Java inwards ascending social club inwards Java
        List<Integer> listing = Arrays.asList( 1, 2, 5, 9, 3, 6, 4, 7, 8);
       
        System.out.println("Unsorted List inwards Java: " + list);
       
        // Sorting List into Java now, Collections.sort()  method volition sort
        // the collection passed
        // to it. Doesn't render anything it only kind the collection itself

        Collections.sort(list); //sorting collection
       
        System.out.println("List inwards Java sorted inwards ascending order: " + list);
       
        // sorting List inwards descending social club inwards Java, Collections.sort() method tin live on used
        // to kind all chemical component subdivision inwards descending social club if nosotros transcend it comparator which tin
        // render descending social club for elements. hither is an instance of sorting List
        // inwards descending social club  in Java
        // Collection shape provides a inwards built-in comparator for that which tin
        // kind objects inwards contrary social club i..e descending social club for Integers or
        // whatever other Object inwards Java
       
        Collections.sort(list, Collections.reverseOrder());
        System.out.println("Java List sorted inwards descending order: " + list);
       
        // Any List implementation inwards Java similar ArrayList, LinkedList
        // tin live on sorted inwards ascending as well as descending social club inwards Java yesteryear next above
        // example, let's run into a quick instance for sorting ArrayList as well as LinkedList
        // inwards ascending as well as descending social club inwards Java
       
       
        //Sorting ArrayList inwards ascending social club inwards Java
        ArrayList alphabets = new ArrayList();
        alphabets.add("c");
        alphabets.add("b");
        alphabets.add("a");
        alphabets.add("d");
       
        System.out.println("Unsorted ArrayList inwards Java : " + alphabets);
       
        //Sorting ArrayList into ascending social club
        Collections.sort(alphabets);
       
        System.out.println("Sorted ArrayList inwards ascending social club inwards Java : " + alphabets);
       
        //Sorting ArrayList into descending social club or contrary social club inwards Java
        Collections.sort(alphabets, Collections.reverseOrder());
       
        System.out.println("ArrayList kind inwards descending social club inwards Java : " + alphabets);      
       
    }  
 
}


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


As shown inwards this kind instance of ArrayList as well as Set, You tin kind LinkedList on ascending as well as descending social club inwards Java. Since Collections.sort() method is applicable to Collection interface, whatever Collection implementation similar Set, HashSet can too live on sorted inwards ascending, descending or contrary social club of elements. Sorting Array inwards Java is completely unlike than sorting collection inwards Java which nosotros volition run into inwards adjacent tutorial. Now you lot learned List , Set and HashSet.

Further Learning
Java Fundamentals: Collections
From Collections to Streams inwards Java 8 Using Lambda Expressions
Grokking Algorithms yesteryear Aditya Bhargava
Java Programming Interview Exposed yesteryear Makham
What is deviation betwixt ArrayList as well as Vector inwards Java
How to iterate over ArrayList inwards Java
How to loop Map inwards Java amongst Example
Difference betwixt ConcurrentHashMap as well as Hashtable inwards Java
Difference betwixt Hashtable as well as HashMap inwards Java

Subscribe to receive free email updates:

0 Response to "How to Sort List into Ascending in addition to Descending Order inward Java"

Posting Komentar