sort an array in descending order code example
Example 1: javascript sort descending
function sortEggsInNest(a, b) {
return a > b ? -1 : b > a ? 1 : 0;
}
Example 2: sorting array in descending order java
// A sample Java program to sort a subarray
// in descending order using Arrays.sort().
import java.util.Arrays;
import java.util.Collections;
public class SortExample
{
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = {13, 7, 6, 45, 21, 9, 2, 100};
// Sorts arr[] in descending order
Arrays.sort(arr, Collections.reverseOrder());
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}