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

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


当前回答

int数组降序排序的简单方法:

private static int[] descendingArray(int[] array) {
    Arrays.sort(array);
    int[] descArray = new int[array.length];
    for(int i=0; i<array.length; i++) {
        descArray[i] = array[(array.length-1)-i];
    }
    return descArray;
}

其他回答

这招对我很管用:

package doublearraysort;

import java.util.Arrays;
import java.util.Collections;

public class Gpa {


    public static void main(String[] args) {
        // initializing unsorted double array
        Double[] dArr = new Double[] {                 
            new Double(3.2),
            new Double(1.2),
            new Double(4.7),
            new Double(3.3),
            new Double(4.6),
           };
        // print all the elements available in list
        for (double number : dArr) {
            System.out.println("GPA = " + number);
        }

        // sorting the array
        Arrays.sort(dArr, Collections.reverseOrder());

        // print all the elements available in list again
        System.out.println("The sorted GPA Scores are:");
        for (double number : dArr) {
            System.out.println("GPA = " + number);
        }
    }
}

输出:

GPA = 3.2
GPA = 1.2
GPA = 4.7
GPA = 3.3
GPA = 4.6
The sorted GPA Scores are:
GPA = 4.7
GPA = 4.6
GPA = 3.3
GPA = 3.2
GPA = 1.2

另一个解决方案是,如果你正在使用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。

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

有一种方法可能会长一点,但它很有效。这是一个对int数组进行降序排序的方法。

希望有一天这能帮助到别人:

public static int[] sortArray (int[] array) {
    int [] sortedArray = new int[array.length];
    for (int i = 0; i < sortedArray.length; i++) {
        sortedArray[i] = array[i];
    }
    
    boolean flag = true;
    int temp;
    while (flag) {
        flag = false;
        for (int i = 0; i < sortedArray.length - 1; i++) {
            if(sortedArray[i] < sortedArray[i+1]) {
                temp = sortedArray[i];
                sortedArray[i] = sortedArray[i+1];
                sortedArray[i+1] = temp;
                flag = true;
            }
        }
    }
    
    return sortedArray;
    
}

这是我如何排序一个基本类型int数组。

int[] intArr = new int[] {9,4,1,7};
Arrays.sort(nums);
Collections.reverse(Arrays.asList(nums));

结果:

[1, 4, 7, 9]
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条件中将">="更改为"<="以使顺序上升。