×

Java Collections Method sort


Collections Method sort

Sort method in collections is used to sort the elemenst of the list . But if you want to apply this method on an array you have to convert the array into list first. You can refer how to convert a list to an array here.

Example

This example shows you how to use sort method of Collection class.

import java.util.*;

public class CollectionsSortMethodDemo {
  public static void main(String[] args)
  {
  String[] things = {"eggs" , "hats" , "laser" , "pie"};
  List list1 = Arrays.asList(things);
  
  
  Collections.sort(list1);
  System.out.printf("%s\n ", list1);
  
  Collections.sort(list1 , Collections.reverseOrder());
  System.out.printf("%s\n ", list1);
  }
}

Output :-

[eggs, hats, laser, pie]
[pie, laser, hats, eggs]