Pages

Monday, August 18, 2025

Sorting an ArrayList in Java - optimized version of sort(use if not asked in question)

 // import java.util.ArrayList;

import java.util.*;
// import java.util.Collections;
public class Classroom {

    public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<>();
       list.add(2);
       list.add(5);
       list.add(9);
       list.add(6);
       list.add(8);

   
       System.out.println("List before sorted: " + list );
       Collections.sort(list);
       System.out.println("List after sorted: " + list);

       // desc order
       Collections.sort(list, Collections.reverseOrder());
       
       // Comparator - fnx logic
       System.out.println("in Desc Order: " + list);
    }

}

No comments:

Post a Comment

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...