2017-06-23 30 views
-2

默认排序方法在Java中不适用于我。用于排序的Java比较器不起作用

int[] nums = {11,14,15,10}; 
    Arrays.sort(nums, new Comparator<Integer>() { 

    public int compare(Integer a, Integer b) { 
     // compare code here 
    } 
}); 

我得到的错误是

"The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], new Comparator<Integer>(){})" 

我觉得我现在是愚蠢的。

+2

int []不是Integer [],而int不是Integer。 – user2357112

+0

您无法指定Comparator通过'Arrays.sort'对int []进行排序。 – saka1029

+0

也许更具体的https://stackoverflow.com/questions/12654600/descending-order。假设在过去的某个时间点,世界上的某个人和你有同样的问题,并从那里开始工作。 – Dukeling

回答

1

没有使用Arrays.sort的方式排除int[]与任何其他顺序,而不是默认值。您可以使用任意ComparatorInteger[]进行排序,但无法为int[]自定义排序。