Skip to content

Sorting

Arrays

To sort an array use

Arrays.sort(arr); // Sorts the array in ascending order

To sort an array in descending order

Arrays.sort(arr,Collections.reverseOrder());

this only works for non-primitive array i.e Integer [] array not int [] array

If we want to sort array from some index to some index use

Arrays.sort(arr,fromIndex,toIndex);

If its a multi-dimensional array

Arrays.sort(arr, (a, b) -> Integer.compare(a[column],b[column]));

to sort an array with custom comparator

CustomComparator implements Comparator<T> {
    public int compare(T c1, T c2)
     {
         // return -1, 0, +1
     }
}

List

To sort a list use

Collections.sort(list);

Comments