-2

Default sort method is not working for me in Java.

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

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

The error I get is

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

I feel like I am stupid now.

user2925213
  • 59
  • 1
  • 5
  • 2
    An `int[]` is not an `Integer[]`, and an `int` is not an `Integer`. – user2357112 Jun 23 '17 at 23:24
  • You can not specify Comparator to sort int[] by `Arrays.sort`. –  Jun 23 '17 at 23:28
  • Perhaps more specifically https://stackoverflow.com/questions/12654600/descending-order. Assume someone in the world has had the same problem as you at some point in the past and work from there. – Bernhard Barker Jun 24 '17 at 00:08

1 Answers1

1

There is no way to use Arrays.sort to sort an int[] with any ordering other than the default. You can sort an Integer[] with an arbitrary Comparator, but there is no way to customize sorting for an int[].

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413