Java Array Reverse sortReversed(final int[] a)

Here you can find the source of sortReversed(final int[] a)

Description

Sorts the specified int array in reversed order.

License

Apache License

Parameter

Parameter Description
a array to be reverse-sorted

Declaration

public static void sortReversed(final int[] a) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    /**/*from w ww  .jav  a2  s  .  com*/
     * Sorts the specified int array in reversed order.
     * 
     * @param a array to be reverse-sorted
     */
    public static void sortReversed(final int[] a) {
        if (a.length <= 1)
            return; // Sorted as it is; also empty array might cause trouble (last = -1 /2)

        // 1. Sort
        Arrays.sort(a);

        // 1. Reverse
        final int last = a.length - 1;

        for (int i = last / 2; i >= 0; i--) {
            // Swap
            final int temp = a[i];
            a[i] = a[last - i];
            a[last - i] = temp;
        }
    }
}

Related

  1. reverseArray(T[] arr)
  2. reverseArrayList(ArrayList listIn)
  3. reverseList (ArrayList l)
  4. reverseRange(final T[] arr, final int from, final int to)
  5. reverseSortedSet(int[] ints)

  6. HOME | Copyright © www.java2s.com 2016