假设我们有以下二维数组:
int camels[][] = new int[n][2];
如何声明Java的Comparator
类来使用Arrays.sort(camels, comparator)
按照第一个元素降序排序数组?参考compare
函数如下:
@Override public int compare(int[] a, int [] b)
{
return b[0] - a[0];
}
假设我们有以下二维数组:
int camels[][] = new int[n][2];
如何声明Java的Comparator
类来使用Arrays.sort(camels, comparator)
按照第一个元素降序排序数组?参考compare
函数如下:
@Override public int compare(int[] a, int [] b)
{
return b[0] - a[0];
}
[...] 如何声明Java Comparator类以按其第一个元素降序对数组进行排序[...]
这是一个使用Java 8的完整示例:
import java.util.*;
public class Test {
public static void main(String args[]) {
int[][] twoDim = { {1, 2}, {3, 7}, {8, 9}, {4, 2}, {5, 3} };
Arrays.sort(twoDim, Comparator.comparingInt(a -> a[0])
.reversed());
System.out.println(Arrays.deepToString(twoDim));
}
}
输出:
[[8, 9], [5, 3], [4, 2], [3, 7], [1, 2]]
针对Java 7,您可以这样做:
Arrays.sort(twoDim, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o2[0], o1[0]);
}
});
如果你不幸在工作中使用的是Java 6或更早版本,则应该执行以下操作:
Arrays.sort(twoDim, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return ((Integer) o2[0]).compareTo(o1[0]);
}
});
int[][] twoDim = { { 1, 2 }, { 3, 7 }, { 8, 9 }, { 4, 2 }, { 5, 3 } };
Arrays.sort(twoDim, (int[] o1, int[] o2) -> o2[0] - o1[0]);
System.out.println(Arrays.deepToString(twoDim));
对我来说,使用Java 8语法是直观且易于记忆的。
我刚刚尝试了这个解决方案,甚至无需编写int。
int[][] twoDim = { { 1, 2 }, { 3, 7 }, { 8, 9 }, { 4, 2 }, { 5, 3 } };
Arrays.sort(twoDim, (a1,a2) -> a2[0] - a1[0]);
java.nio.IntBuffer#wrap(int[])
提供了一种优秀的内置方法来比较两个 int[]
实例,因为 IntBuffer
既是一个轻量级的 int[]
实例包装器,又实现了 Comparable
接口。与其他答案中的示例相比,将其与其他内置的 Comparator
特性结合使用具有以下几个优点:
null
数组元素此示例按降序对数组进行排序,将 null
数组元素放在最后:
int[][] twoDim = {{1, 2}, {3, 7}, {8, 9}, {4, 2}, null, {5, 3}, {4}};
System.out.println("Unsorted: " + Arrays.deepToString(twoDim));
Comparator<int[]> c = Comparator.nullsFirst(Comparator.comparing(IntBuffer::wrap));
Arrays.sort(twoDim, c.reversed());
System.out.println("Sorted: " + Arrays.deepToString(twoDim));
输出:
Unsorted: [[1, 2], [3, 7], [8, 9], [4, 2], null, [5, 3], [4]]
Sorted: [[8, 9], [5, 3], [4, 2], [4], [3, 7], [1, 2], null]
这里是对日期数组进行排序的ArrayList。 也许有人会在某个时候需要它。
List <Date[]> sortedDateList = new ArrayList<>(/* initialization */);
Collections.sort(sortedDateList, new Comparator<Date[]>() {
@Override
public int compare(Date[] d1, Date[] d2) {
return (d1[0].compareTo(d2[0]));
}
});
int
上调用.compareTo
。 - aioobesort() {$1 > $2}
。 - Zhou Haibo