Java 从高到低对 int 数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20251762/
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-08-13 00:35:27  来源:igfitidea点击:

Sorting an int array from highest to lowest

javaarrayssorting

提问by lukeb28

So I just learned about the Arrays.sort(arrayName);and applied it in a project of mine but I found it sorts from lowest to highest. Is there anyway to have it do the opposite? (I don't think I need to post the code in question but if it is needed I'll post it).

所以我刚刚了解了它Arrays.sort(arrayName);并将其应用于我的一个项目,但我发现它从最低到最高排序。无论如何让它做相反的事情吗?(我认为我不需要发布有问题的代码,但如果需要,我会发布)。

采纳答案by Rohit Jain

If you use an Integer[]instead of int[], then you can pass a Comparatoras 2nd argument to the sort method. To impose reverse ordering, you can make use of Collections.reverseOrder()method:

如果您使用一个Integer[]而不是int[],那么您可以将 aComparator作为第二个参数传递给 sort 方法。要强加反向排序,您可以使用Collections.reverseOrder()方法:

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

回答by Sage

  1. Use an Integer[]instead of int[]
  2. Make use of Collections.reverseOrder(): Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface
  3. If possible use ArrayList<Integer>and Collections.sort(list, Collections.reverseOrder())for stronger case.

    Integer[] intArr = new Integer[10];
     // add some integer 
    Arrays.sort(intArr, Collections.reverseOrder())
    
  1. 使用Integer[]代替int[]
  2. 使用Collections.reverseOrder():返回一个比较器,该比较器对实现 Comparable 接口的对象集合强加自然顺序的反向
  3. 如果可能,请使用ArrayList<Integer>Collections.sort(list, Collections.reverseOrder())用于更强的情况。

    Integer[] intArr = new Integer[10];
     // add some integer 
    Arrays.sort(intArr, Collections.reverseOrder())
    

回答by Alexis C.

If you have an int[]array, you can sort it using Arrays.sortand then reverse it :

如果您有一个int[]数组,则可以使用对它进行排序Arrays.sort,然后将其反转:

int [] tab2 = new int[]{1,5,0,-2};
Arrays.sort(tab2);
ArrayUtils.reverse(tab2);
System.out.print(Arrays.toString(tab2));

Output :

输出 :

[5, 1, 0, -2]

Codeof the reverse method (from org.apache.commons.lang.ArrayUtils.reverse(int[])) :

Code反向方法(来自org.apache.commons.lang.ArrayUtils.reverse(int[])):

public static void reverse(int[] array) {
        if (array == null) {
            return;
        }
        int i = 0;
        int j = array.length - 1;
        int tmp;
        while (j > i) {
            tmp = array[j];
            array[j] = array[i];
            array[i] = tmp;
            j--;
            i++;
        }
}

回答by Gyro Gearless

For some usecases you may come along by just treating your sorted array as "reverse sorted". E.g. to iterate from highest to lowest number you may use

对于某些用例,您可能只是将排序的数组视为“反向排序”。例如,从您可能使用的最高数字到最低数字进行迭代

int[] foo = ...;
Arrays.sort(foo);
for (int i=foo.length-1; i>=0; i--) { 
  doSomethingWith(foo[i]);
 }