arrays.sort in java reverse code example

Example 1: reverse array in java

int length = array.length;
		for(int i=0;i<length/2;i++) {
			int swap = array[i];
			array[i] = array[length-i-1];
			array[length-i-1] = swap;
		}
or
Collections.reverse(Arrays.asList(array));

Example 2: java sort array descending

Integer[] cubes = new Integer[] { 8, 27, 64, 125, 256 };
Arrays.sort(cubes, Collections.reverseOrder());

Read more: https://www.java67.com/2016/07/how-to-sort-array-in-descending-order-in-java.html#ixzz6MX60xZLb

Example 3: java sort method

Arrays.sort();//this method is an inbuild function in java
//to sort the array

Example 4: 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)); 
    } 
}

Tags:

Misc Example