有什么简单的方法来降序排序一个数组,就像他们有一个升序排序在数组类?

还是说我必须停止懒惰,自己做这件事:[


当前回答

我有下面的工作解决方案

    public static int[] sortArrayDesc(int[] intArray){
    Arrays.sort(intArray);                      //sort intArray in Asc order
    int[] sortedArray = new int[intArray.length];   //this array will hold the sorted values

    int indexSortedArray = 0;
    for(int i=intArray.length-1 ; i >= 0 ; i--){    //insert to sortedArray in reverse order
        sortedArray[indexSortedArray ++] = intArray [i];
    }
    return sortedArray;
}

其他回答

这里有很多乱七八糟的东西——人们建议非原始值的解决方案,尝试从基础上实现一些排序算法,给出涉及额外库的解决方案,炫耀一些俗套的解决方案等等。最初问题的答案是50/50。对于那些只想复制/粘贴的人:

// our initial int[] array containing primitives
int[] arrOfPrimitives = new int[]{1,2,3,4,5,6};

// we have to convert it into array of Objects, using java's boxing
Integer[] arrOfObjects = new Integer[arrOfPrimitives.length];
for (int i = 0; i < arrOfPrimitives.length; i++) 
    arrOfObjects[i] = new Integer(arrOfPrimitives[i]);

// now when we have an array of Objects we can use that nice built-in method
Arrays.sort(arrOfObjects, Collections.reverseOrder());

arrOfObjects现在是{6,5,4,3,2,1}。如果你有一个不是整数的数组——使用相应的对象而不是整数。

在这里加上我对几个不同场景的答案 对于数组

Arrays.sort(a, Comparator.reverseOrder());

FWIW列表

Lists.reverse(a);

任何及所有收集

Collections.reverse(a);

我知道这是一个相当老的线程,但这里是一个更新版本的整数和Java 8:

Arrays.sort(array, (o1, o2) -> o2 - o1);

注意,对于正常的升序(或Comparator.comparingInt()),它是“o1 - o2”。

这也适用于任何其他类型的对象。说:

Arrays.sort(array, (o1, o2) -> o2.getValue() - o1.getValue());

对于包含原语元素的数组,如果有org.apache.commons.lang(3)可供处置,则简单的反向数组(排序后)的方法是使用:

ArrayUtils.reverse(array);

有时我们练习一个例子是很好的,这里有一个完整的例子:

sortdesc.java

import java.util.Arrays;
import java.util.Collections;
class sortdesc{
public static void main(String[] args){
       // int Array
       Integer[] intArray=new Integer[]{
                 new Integer(15),
                 new Integer(9),
                 new Integer(16),
                 new Integer(2),
                 new Integer(30)};

       // Sorting int Array in descending order
       Arrays.sort(intArray,Collections.reverseOrder());

       // Displaying elements of int Array
       System.out.println("Int Array Elements in reverse order:");
       for(int i=0;i<intArray.length;i++)
          System.out.println(intArray[i]);

       // String Array
       String[] stringArray=new String[]{"FF","PP","AA","OO","DD"};

       // Sorting String Array in descending order
       Arrays.sort(stringArray,Collections.reverseOrder());

       // Displaying elements of String Array
       System.out.println("String Array Elements in reverse order:");
       for(int i=0;i<stringArray.length;i++)
          System.out.println(stringArray[i]);}}

编译…

javac sortdec.java

叫它……

java sortdesc

输出

Int Array Elements in reverse order:
30
16
15
9
2
String Array Elements in reverse order:
PP
OO
FF
DD
AA

如果你想尝试字母数字数组…

//replace this line:
String[] stringArray=new String[]{"FF","PP","AA","OO","DD"};

//with this:
String[] stringArray=new String[]{"10FF","20AA","50AA"};

你会得到如下输出:

50AA
20AA
10FF