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

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


当前回答

public double[] sortArrayAlgorithm(double[] array) { //sort in descending order
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array.length; j++) {
            if (array[i] >= array[j]) {
                double x = array[i];
                array[i] = array[j];
                array[j] = x;
            }
        }
    }
    return array;
}

只需使用此方法按降序对double类型的数组进行排序,您可以使用它对任何其他类型的数组(如int, float等)进行排序,只需将“返回类型”,“参数类型”和变量“x”类型更改为相应的类型。您还可以在if条件中将">="更改为"<="以使顺序上升。

其他回答

另一个解决方案是,如果你正在使用Comparable接口,你可以切换你在compareTo(Object bcomparable)中指定的输出值。

例如:

public int compareTo(freq arg0) 
{
    int ret=0;
    if(this.magnitude>arg0.magnitude)
        ret= 1;
    else if (this.magnitude==arg0.magnitude)
        ret= 0;
    else if (this.magnitude<arg0.magnitude)
        ret= -1;
    return ret;
}

在我的程序中,量级是一个具有双数据类型的属性。这是对我定义的类频率按大小的倒序排序。因此,为了纠正这一点,您可以切换<和>返回的值。这将为您提供以下内容:

public int compareTo(freq arg0) 
{
    int ret=0;
    if(this.magnitude>arg0.magnitude)
        ret= -1;
    else if (this.magnitude==arg0.magnitude)
        ret= 0;
    else if (this.magnitude<arg0.magnitude)
        ret= 1;
    return ret;
}

为了使用这个compareTo,我们简单地调用Arrays.sort(mFreq),它会给你排序的数组freq [] mFreq。

这个解决方案的美妙之处在于(在我看来),它可以用来对用户定义的类进行排序,甚至可以根据特定的属性对它们进行排序。如果可比接口的实现听起来让你望而生畏,我建议你不要这么想,事实并非如此。这个关于如何实现可比性的链接让我的事情变得简单多了。希望人们可以利用这个解决方案,你的快乐甚至可以和我的一样。

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

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

不能直接使用Arrays.sort()和Collections.reverseOrder()对原语数组(即int[] arr ={1,2,3};)进行反向排序,因为这些方法需要引用类型(Integer)而不是原语类型(int)。

但是,我们可以使用Java 8 Stream首先对数组进行装箱,以倒序排序:

// an array of ints
int[] arr = {1, 2, 3, 4, 5, 6};

// an array of reverse sorted ints
int[] arrDesc = Arrays.stream(arr).boxed()
    .sorted(Collections.reverseOrder())
    .mapToInt(Integer::intValue)
    .toArray();

System.out.println(Arrays.toString(arrDesc)); // outputs [6, 5, 4, 3, 2, 1]

你可以使用这个对所有类型的对象进行排序

sort(T[] a, Comparator<? super T> c) 

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

arrays .sort()不能直接用于按降序对基本数组进行排序。如果试图通过传递Collections.reverseOrder()定义的反向Comparator来调用Arrays.sort()方法,它将抛出错误

没有合适的sort方法(int[],comparator)

这将很好地工作与“对象数组”,如Integer Array,但将不适用于原始数组,如int Array。

按降序对原始数组排序的唯一方法是,首先按升序对数组排序,然后在适当的位置反转数组。这对于二维基元数组也是成立的。

你可以用这个:

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

Collections.reverseOrder()返回一个使用逆自然顺序的比较器。你可以使用Collections.reverseOrder(myComparator)来获得你自己的比较器的反向版本。