ArrayList.sort(Comparator<? super E>): This method is available in java.util.ArrayList class of Java.
Syntax:
void java.util.ArrayList.sort(Comparator<? super Integer> c)
This method sorts this list according to the order induced by the specified Comparator.
Note: The sort is stable: this method must not reorder equal elements.
Parameters: One parameter is required for this method.
c: the Comparator used to compare list elements. A null value indicates that the elements' natural ordering should be used.
Exceptions: NA
Approach
Java
import java.util.ArrayList;import java.util.Collections;public class ArrayListsort {public static void main(String[] args) {ArrayList<Integer> arr = new ArrayList<>();arr.add(10);arr.add(234);arr.add(32);arr.add(3);System.out.println("Array Before sort: ");arr.forEach((n -> System.out.print(n + " ")));System.out.println();Collections.sort(arr);System.out.println("Array After sort: ");arr.forEach((n -> System.out.print(n + " ")));}}
Output:
Array Before sort:
10 234 32 3
Array After sort:
3 10 32 234
No comments:
Post a Comment