Collections.sort(List): This method is available in java.util.Collections class of Java.
Syntax:
<E> void java.util.Collections.sort(List<E> list)
This method takes one argument. This method sorts the specified list into ascending order, according to the natural ordering of its elements.
Parameters: One parameter is required for this method.
list: the list to be sorted.
Throws:
1. ClassCastException - if the list contains elements that are not mutually comparable.
2. UnsupportedOperationException - if the specified list'slist-iterator does not support the set operation.
3. IllegalArgumentException - if the implementation detects that the natural ordering of the list elements is found to violate the Comparable contract.
Approach
Java
import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Collectionssort {public static void main(String[] args) {List<Integer> arrlist = new ArrayList<Integer>();arrlist.add(12);arrlist.add(56);arrlist.add(899);arrlist.add(65);arrlist.add(5);Collections.sort(arrlist);System.out.println(arrlist);}}
Output:
[5, 12, 56, 65, 899]
No comments:
Post a Comment