Java数组降序排序?

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

Java Array Sort descending?

javaarrayssortingint

提问by AFK

Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class?

有没有什么简单的方法可以按降序对数组进行排序,就像它们在Arrays 类中按升序排序一样?

Or do I have to stop being lazy and do this myself :[

或者我必须停止懒惰并自己做这个:[

采纳答案by AFK

You could use this to sort all kind of Objects

您可以使用它对所有类型的对象进行排序

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

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

Arrays.sort()cannot be used directly to sort primitive arrays in descending order. If you try to call the Arrays.sort()method by passing reverse Comparator defined by Collections.reverseOrder(), it will throw the error

Arrays.sort()不能直接用于按降序对原始数组进行排序。如果您尝试Arrays.sort()通过传递由 定义的反向比较器来调用该方法Collections.reverseOrder(),则会抛出错误

no suitable method found for sort(int[],comparator)

找不到适合 sort(int[],comparator) 的方法

That will work fine with 'Array of Objects' such as Integer array but will not work with a primitive array such as int array.

这将适用于“对象数组”,例如整数数组,但不适用于原始数组,例如 int 数组。

The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place. This is also true for two-dimensional primitive arrays.

按降序对原始数组进行排序的唯一方法是,首先按升序对数组进行排序,然后将数组原地反转。对于二维原始数组也是如此。

回答by William

You can use this:

你可以使用这个:

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

Collections.reverseOrder()returns a Comparatorusing the inverse natural order. You can get an inverted version of your own comparator using Collections.reverseOrder(myComparator).

Collections.reverseOrder()Comparator使用逆自然顺序返回 a 。您可以使用Collections.reverseOrder(myComparator).

回答by Ornithopter

for a list

一份清单

Collections.sort(list, Collections.reverseOrder());

for an array

对于数组

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

回答by Josip Maslac

For array which contains elements of primitives if there is org.apache.commons.lang(3)at disposal easy way to reverse array (after sorting it) is to use:

对于包含基元元素的数组,如果可以使用org.apache.commons.lang(3)简单的方法来反转数组(排序后),请使用:

ArrayUtils.reverse(array);

回答by SK9

I don't know what your use case was, however in addition to other answers here another (lazy) option is to still sort in ascending order as you indicate but then iterate in reverseorder instead.

我不知道您的用例是什么,但是除了这里的其他答案之外,另一个(懒惰的)选项仍然是按照您的指示按升序排序,然后以相反的顺序迭代。

回答by FHDougherty

an alternative could be (for numbers!!!)

另一种可能是(对于数字!!!)

  1. multiply the Array by -1
  2. sort
  3. multiply once again with -1
  1. 将数组乘以 -1
  2. 种类
  3. 再次乘以-1

Literally spoken:

字面意思:

array = -Arrays.sort(-array)

回答by Milan

without explicit comparator:

没有显式比较器:

Collections.sort(list, Collections.reverseOrder());

with explicit comparator:

使用显式比较器:

Collections.sort(list, Collections.reverseOrder(new Comparator()));

回答by Michel Jung

Java 8:

爪哇 8:

Arrays.sort(list, comparator.reversed());

Update: reversed()reverses the specified comparator. Usually, comparators order ascending, so this changes the order to descending.

更新: reversed()反转指定的比较器。通常,比较器按升序排列,因此这会将顺序更改为降序。

回答by Masoud

First you need to sort your array using:

首先,您需要使用以下方法对数组进行排序:

Collections.sort(Myarray);

Then you need to reverse the order from ascending to descending using:

然后,您需要使用以下方法将顺序从升序反转为降序:

Collections.reverse(Myarray);

回答by Chris - Jr

Anothersolution is that if you're making use of the Comparableinterface you can switch the output values which you had specified in your compareTo(Object bCompared).

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

For Example :

例如 :

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;
}

Where magnitudeis an attribute with datatype doublein my program. This was sorting my defined class freqin reverse order by it's magnitude. So in order to correct that, you switch the values returned by the <and >. This gives you the following :

其中幅度是与数据类型的属性,双击在我的计划。这是按大小以相反的顺序对我定义的类频率进行排序。所以为了纠正这一点,你切换由返回的值<>。这为您提供以下内容:

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;
}

To make use of this compareTo, we simply call Arrays.sort(mFreq)which will give you the sorted array freq [] mFreq.

为了使用这个 compareTo,我们只需调用Arrays.sort(mFreq)which 会给你排序的数组freq [] mFreq

The beauty (in my opinion) of this solution is that it can be used to sort user defined classes, and even more than that sort them by a specific attribute. If implementation of a Comparable interface sounds daunting to you, I'd encourage you not to think that way, it actually isn't. This link on how to implement comparablemade things much easier for me. Hoping persons can make use of this solution, and that your joy will even be comparableto mine.

这个解决方案的美妙之处(在我看来)在于它可以用于对用户定义的类进行排序,甚至比按特定属性对它们进行排序还多。如果 Comparable 接口的实现对您来说听起来令人生畏,我鼓励您不要那样想,实际上并非如此。这个关于如何实现可比性的链接让我更容易。希望大家能用上这个方案,希望你们的快乐和我的一样。