简体   繁体   English

用于在 Java 中排序 2D arrays 和 1D arrays 的比较器

[英]Comparator for sorting 2D arrays and 1D arrays in Java

I used the following code for sorting a 2D array of int[][] type in reverse order by making use of a comparator.我使用以下代码通过使用比较器以相反的顺序对 int[][] 类型的二维数组进行排序。

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

But I am unable to sort a 1D array of int[] type using similar approach.但是我无法使用类似的方法对 int[] 类型的一维数组进行排序。 On the internet I found information saying "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."在互联网上,我发现信息说“按降序对原始数组进行排序的唯一方法是,首先按升序对数组进行排序,然后将数组反转到位。”

Why am I able to sort a 2D array of primitive type, but not a 1D array using comparator?为什么我能够对原始类型的二维数组进行排序,但不能使用比较器对一维数组进行排序?

It is possible to sort an int[][] in descending order because one basically compares int[] s against each others.可以按降序对int[][]进行排序,因为基本上是将int[]相互比较。 As per JLS, §10 : " ... Arrays are objects... ".根据JLS,§10 :“ ... Arrays 是对象... ”。

Looking closer at the Arrays API, we find method sort(T[], Comparator<? super T>) , which, together with some static builder methods from Comparator , allows to reverse-sort an array of objects:仔细Arrays API,我们发现方法sort(T[], Comparator<? super T>) ,它与一些 static 对象的排序构建器方法一起,允许从Comparator :

T[] someArray =  ...
Arrays.sort(someArray, Comparator.<T>naturalOrder().reversed())

Ideone Demo Ideone 演示

This only works for object-arrays, not for primitive arrays.这仅适用于对象数组,不适用于原始 arrays。 And for primitive arrays, we do not have any method sort(int[], Comparator<...>) in Arrays (probably because one cannot use primitives as generic types, project Valhalla may or may not change this in the future).对于原语 arrays,我们在Arrays中没有任何方法sort(int[], Comparator<...>) (可能是因为不能将原语用作泛型类型, Valhalla 项目将来可能会也可能不会改变这一点)。

So yes, sorting an array of primitives and then reversing it seems like the only option if one wants to have constant memory overhead.所以是的,如果一个人想要有恒定的 memory 开销,那么对一组基元进行排序然后反转它似乎是唯一的选择。 It would look something like this (sketch):它看起来像这样(草图):

final int[] values = { 1, 5, 3, 2, 4 };
Arrays.sort(values);
reverse(values);

Ideone Demo Ideone 演示

You can use Stream to convert an int to an Integer , do sort using a Comparator and reconvert it to an int[] .您可以使用Streamint转换为Integer ,使用Comparator进行排序并将其重新转换为int[]

final int[] values = {2, 0, 5, 1, 3, 4};
int[] reversed = IntStream.of(values)
    .boxed()
    .sorted(Comparator.reverseOrder())
    .mapToInt(i -> i)
    .toArray();
System.out.println(Arrays.toString(reversed));

output: output:

[5, 4, 3, 2, 1, 0]

There just aren't any built-in sort methods that accept a 1D primitive array and a Comparator .没有任何内置的排序方法可以接受一维原始数组和Comparator

As for why , only the designers can say authoratively, but here are some arguments against having them:至于为什么,只有设计师可以权威地说,但这里有一些 arguments 反对拥有它们:

  • Primitive arrays are not used very often in Java programs to begin with.原语 arrays 在 Java 程序中并不经常使用。
  • A sort implementation that uses Comparator would need to wrap every array element in an object to pass them to the Comparator anyway, so you might as well have the user convert the array of int into an array of Integer themselves.使用Comparatorsort实现需要将每个数组元素包装在 object 中以将它们传递给Comparator ,因此您不妨让用户自己将int数组转换为Integer数组。
  • You would need to add 7 or 14 more Arrays.sort implementations which is a non-trivial amount of code to test您需要再添加 7 或 14 个Arrays.sort实现,这是要测试的大量代码
  • The most common use case for a custom comparator is sorting in reverse, and you can already achieve that by first sorting and then reversing自定义比较器最常见的用例是反向排序,您已经可以通过先排序然后反向来实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM