使用 lambda 进行数组排序

huangapple go评论62阅读模式
英文:

Sort arrays using lambda

问题

我想要逆序排列 Java 8 中的数组。
我不想使用流操作。

这里有一个例子:

int[] a = {1, 2, 3, 4, 5};

我希望它变成这样:{5, 4, 3, 2, 1};

因此我的代码是这样的:

Arrays.sort(a, (o1, o2) -> o2 - o1);

Arrays.sort(arr, (a, b) -> Integer.compare(b, a));

但是我收到了一个错误信息:

无法对 'T' 应用运算符 '-'

如何进行逆序排序和排序?

有没有更好的方法?

英文:

I want revers-sort java8.
I don't want use stream.

Here is an example:

int[] a = {1, 2, 3, 4, 5};

I want it like this: {5, 4, 3, 2, 1};

So my code is this

Arrays.sort(a, (o1, o2) -> o2 - o1);

Arrays.sort(arr, (a,b) ->Integer.compare(b ,a));

But I get an error message:

> Operator '-' cannot be applied to 'T', 'T'

How can I reverse sort and sort?

Is there a better way?

答案1

得分: 2

你可以尝试

int[] sortedArray = Arrays.stream(a)
        .boxed()
        .sorted(Comparator.reverseOrder())
        .mapToInt(Integer::intValue)
        .toArray();

如果你使用整数数据类型,那就更简单了

Arrays.sort(a, Comparator.reverseOrder());
英文:

You can try

int[] sortedArray = Arrays.stream(a)
        .boxed()
        .sorted(Comparator.reverseOrder())
        .mapToInt(Integer::intValue)
        .toArray();

if you use Integer data type then even easier

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

答案2

得分: 1

你正在尝试使用 public static <T> void sort(T[] a, Comparator<? super T> c) 方法,但你将一个原始类型数组传递给它。原始类型不能替代泛型类型参数。

如果你使用 Integer[] 代替 int[],你的代码将会正常工作。

Integer[] arr = {1, 2, 3, 4, 5};
Arrays.sort(arr, (a, b) -> b - a);

或者

Integer[] arr = {1, 2, 3, 4, 5};
Arrays.sort(arr, (a, b) -> Integer.compare(b, a));
英文:

You are trying to use public static &lt;T&gt; void sort(T[] a, Comparator&lt;? super T&gt; c), but you pass to it a primitive array. Primitive types cannot be used in place of generic type parameters.

If you use Integer[] instead of int[], your code will work.

Integer[] arr = {1, 2, 3, 4, 5};
Arrays.sort(arr, (a,b) -&gt; b - a);

or

Integer[] arr = {1, 2, 3, 4, 5};
Arrays.sort(arr, (a,b) -&gt; Integer.compare(b,a));

答案3

得分: 0

你需要将int[]更改为Integer[]

Integer[] a = {1, 2, 3, 4, 5};
Arrays.sort(a, (o1, o2) -> o2 - o1);

这是方法签名,对于基本类型不起作用。

public static <T> void sort(T[] a, Comparator<? super T> c);

你可能想知道为什么它适用于int[]。这是因为还有另一个接受基本类型int[]的方法。

public static void sort(int[] a);
英文:

You need to change int[] to Integer[].

Integer[] a = {1, 2, 3, 4, 5};
Arrays.sort(a, (o1, o2) -&gt; o2 - o1);

This is the method signature which doesn't works for primitive type.

public static &lt;T&gt; void sort(T[] a, Comparator&lt;? super T&gt; c);

You might be wondering why it works for int[]. Its because there is another method which accepts primitive type int[]

public static void sort(int[] a);

答案4

得分: 0

如果您可以使用Integer代替int,只需使用以下代码。

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Integer[] a = {1, 2, 3, 4, 5};
        Arrays.sort(a, (o1, o2) -> o2 - o1);

        for (Integer i : a)
            System.out.print(i + ", ");
    }
}

其输出将是:

5, 4, 3, 2, 1,
英文:

If you can use Integer instead of int it would be great simply use the below code.

import java.util.Arrays;

public class Main
{
    public static void main(String[] args) {
	    Integer[] a = {1, 2, 3, 4, 5};
	    Arrays.sort(a, (o1, o2) -&gt; o2 - o1);

        for(Integer i: a)
            System.out.print(i+&quot;, &quot;);
    }
}

Its output will be:

5, 4, 3, 2, 1,

huangapple
  • 本文由 发表于 2020年9月17日 20:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63937630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定