Sunday, June 22, 2025
HomeJavaThe best way to Kind a Checklist into Ascending and Descending Order...

The best way to Kind a Checklist into Ascending and Descending Order in Java? Examples


        // to it. Does not
return something it simply types the gathering itself

        Collections.type(record); //sorting
assortment

       
        System.out.println(“Checklist in Java sorted in ascending order: “
+ record);
       
        // sorting Checklist in
descending order in Java, Collections.type() methodology can be utilized

        // to type all
ingredient in descending order if we move it comparator which may

        // return descending
order for parts. right here is an instance of sorting Checklist

        // in descending
order  in Java

        // Assortment class
gives a in built-in comparator for that which may

        // type objects in
reverse order i..e descending order for Integers or

        // another Object
in Java

       
        Collections.type(record, Collections.reverseOrder());
        System.out.println(“Java Checklist sorted in descending order: “ +
record);
       
        // Any Checklist
implementation in Java like ArrayList, LinkedList

        // will be sorted in
ascending and descending order in Java by following above

        // instance, let’s
see a fast instance 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.type(alphabets);
       
        System.out.println(“Sorted ArrayList in ascending order in Java : “
+ alphabets);

       
        //Sorting ArrayList
into descending order or reverse order in Java

        Collections.type(alphabets, Collections.reverseOrder());
       
        System.out.println(“ArrayList type in descending order in Java : “
+ alphabets);
     
       
    }  
 
}

Output:
Unsorted Checklist in Java: [1, 2, 5, 9, 3, 6, 4, 7, 8]
Checklist in Java sorted in ascending order: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Java Checklist 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 type in descending order in Java : [d,
c, b, a]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments