java 数组排序降序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31901543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:19:29  来源:igfitidea点击:

Array Sorting Descending Order

javaarrayssorting

提问by Hugo Diaz

I'm a new programmer to java, been using this website a lot to learn the ways. Today I've stumbled into another problem towards a program I'm doing for practice.

我是 Java 的新程序员,经常使用这个网站来学习方法。今天,我在为练习而做的一个程序中遇到了另一个问题。

I have an array:

我有一个数组:

    final int EXAMS = 5;
    int[] scores = new int [EXAMS];

The values for this array are then asked from the user through a Scanner object:

然后通过 Scanner 对象向用户询问此数组的值:

for (int index = 0; index < EXAMS; index++)
    {
        System.out.println("Enter the score for " + (index+1) +":");
        scores[index] = kb.nextInt();
        if (scores[index] < 0){
            System.out.println("The number you have entered is invalid.");
            scores[index] = kb.nextInt();
        }
    }

I managed to make an ascending order sorting of the values "scores[]":

我设法对值“scores[]”进行升序排序:

       Arrays.sort(scores);
        System.out.println("The sorted int array is:");
        for (int number : scores) 
        {
            System.out.println("Number = "+ number);
        }

But I want the sorting to be in descending order. When I put the

但我希望排序按降序排列。当我把

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

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

I get an error saying: "no suitable method found for sorting." Please help.

我收到一条错误消息:“找不到合适的排序方法。” 请帮忙。

采纳答案by Eran

There's no sortmethod in Arraysclass that accepts an int[]and a Comparator. The sortmethods in the Arraysclass that accept a Comparatorrequire an array of reference type (while an int[]is an array of a primitive type).

有没有sort在方法Arrays的类,它接受int[]Comparator。所述sort的方法中Arrays类接受一个Comparator需要引用类型的阵列(而一个int[]是基本类型的阵列)。

If you change the type of your scoresarray from int[]to Integer[], your code will work, since the method public static <T> void sort(T[] a, Comparator<? super T> c)of the Arraysclass will match your Arrays.sort(scores, Collections.reverseOrder());call.

如果你改变你的类型scores的数组int[]Integer[],你的代码将工作,因为该方法public static <T> void sort(T[] a, Comparator<? super T> c)的的Arrays类将符合您的Arrays.sort(scores, Collections.reverseOrder());通话。

final int EXAMS = 5;
Integer[] scores = new Integer [EXAMS]; // the only required change
for (int index = 0; index < EXAMS; index++) {
    System.out.println("Enter the score for " + (index+1) +":");
    scores[index] = kb.nextInt();
    if (scores[index] < 0){
        System.out.println("The number you have entered is invalid.");
        scores[index] = kb.nextInt();
    }
}
Arrays.sort(scores, Collections.reverseOrder());
System.out.println("The sorted int array is:");
for (int number : scores)  {
    System.out.println("Number = "+ number);
}

回答by alfasin

The following works (using the IntegerObjects instead of primitive int):

以下工作(使用IntegerObjects 而不是 original int):

Integer[] test = {1, 2, 4, 3, 5};
Arrays.sort(test, Collections.reverseOrder());
System.out.println(Arrays.toString(test)); // [5, 4, 3, 2, 1]

回答by Danny Varela

Collections.reverseOrder() 

Won't work because the array is of primitive type. You can change from int to Integer or try something different like:

不会工作,因为数组是原始类型。您可以将 int 更改为 Integer 或尝试不同的方法,例如:

Arrays.sort(scores);
ArrayUtils.reverse(scores);

Your final option would be to make your own code.

您的最终选择是制作自己的代码。

回答by Nitesh Virani

You just need to change your array type from Primitive typeto Primitive wrapper classi.e. int[] scores = new int [EXAMS]to Integer[] scores = new Integer[EXAMS]

你只需要改变你的数组类型从Primitive typePrimitive wrapper classieint[] scores = new int [EXAMS]Integer[] scores = new Integer[EXAMS]

Arrays.sortaccept wrapper array in the first argument not primitive.

Arrays.sort在第一个参数中接受不是原始参数的包装器数组。

回答by Parag Kadam

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

does not work with primitives. Try this,

不适用于原语。试试这个,

Integer[] a = new Integer[]{1,2,3,5,4};
            Arrays.sort(a,Collections.reverseOrder());
            for(int i:a)
            {
                System.out.println(i);
            }

回答by pramod kumar

There is no sort method for arrays in java.But ArrayList have a sort method.You can the below code to sort the list in reverse order.

java中没有数组的排序方法。但是ArrayList有排序方法。可以通过下面的代码对列表进行逆序排序。

List l=new ArrayList();
l.add(5);
l.add(1);
l.add(2);
l.add(3);
Collections.sort(l,Collections.reverseOrder());
System.out.println(l);