Q:

how to sort a list in java

import java. util. Arrays;
Arrays. sort(array);
14
Here’s the java program to sort an array using Arrays.sort() method.

import java.util.Arrays;
public class JavaArraySortMethod
{
   public static void main(String[] args)
   {
      String[] strGiven = {"Great Barrier Reef", "Paris", "borabora", "Florence","tokyo", "Cusco"};
      Arrays.sort(strGiven);
      System.out.println("Output(case sensitive) : " + Arrays.toString(strGiven));
   }
}
5
Collections.sort(testList);
Collections.reverse(testList);
2
Arrays.sort();//this method is an inbuild function in java
//to sort the array
4
// Java program to demonstrate working of Collections.sort() 
import java.util.*; 
  
public class Collectionsorting 
{ 
    public static void main(String[] args) 
    { 
        // Create a list of strings 
        ArrayList<String> al = new ArrayList<String>(); 
        al.add("Geeks For Geeks"); 
        al.add("Friends"); 
        al.add("Dear"); 
        al.add("Is"); 
        al.add("Superb"); 
  
        /* Collections.sort method is sorting the 
        elements of ArrayList in ascending order. */
        Collections.sort(al); 
  
        // Let us print the sorted list 
        System.out.println("List after the use of" + 
                           " Collection.sort() :\n" + al); 
    } 
} 
2
import Collections

ArrayList< int > a = new ArrayList();
a.add(1);
a.add(3);
a.add(2);
a.add(-3);

ArrayList< int > sorted = Collections.sort(a);
-2

New to Communities?

Join the community