【问题标题】:Java comparator for sorting not working [duplicate]用于排序的Java比较器不起作用[重复]
【发布时间】:2017-06-23 23:23:17
【问题描述】:

默认排序方法在 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>(){})"

我觉得我现在很愚蠢。

【问题讨论】:

  • int[] 不是Integer[]int 不是Integer
  • 您不能指定 Comparator 以通过 Arrays.sort 对 int[] 进行排序。
  • 也许更具体地说是stackoverflow.com/questions/12654600/descending-order。假设世界上有人在过去的某个时候遇到过与您相同的问题并从那里开始工作。

标签: java sorting comparator


【解决方案1】:

没有方法可以使用Arrays.sortint[] 进行排序,而不是默认值。您可以使用任意ComparatorInteger[] 进行排序,但无法自定义int[] 的排序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多