3

My code is shown below:

public class Solution {
    public void nextPermutation(int[] nums) {
        int k = 0;
        for(int i = nums.length -1; i> 0 ;i--){
            if(nums[i-1] < nums[i]){
                k = i-1;
                break;
            }
        }
        if( k == 0) {Arrays.sort(nums); return;}
        int tmp = nums[k];
        nums[k] = nums[nums.length - 1];
        nums[nums.length-1] = tmp;
        Arrays.sort(nums,k+1,nums.length,new Comparator<Integer>(){
            public int compare(Integer a, Integer b){
                return b - a;
            }
        });
    }    
}

I want to sort the array in decreasing order by using comparator, but it always shows

Line 14: error: no suitable method found for sort(int[],int,int, anonymous Comparator)

Can anyone point out where is the problem? Thanks a lot!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
henryzbw
  • 33
  • 4
  • @cricket_007 ...How is this a duplicate? This question asks about a compiler error, that one asks how to speed up an algorithm, and the answers to both are most definitely not the same. – Nic Oct 05 '16 at 22:13
  • @QPaysTaxes The error is because primitives are attempted to be sorted, yes? – OneCricketeer Oct 05 '16 at 22:15
  • @cricket_007 That's not the criteria for duplicate. Look at the answers of the question you linked -- do they answer the question directly? Sure, you could get _an_ answer, but the key problem -- that `int` and `Integer` aren't the same type, and `int`s can't be used in generic methods at all -- isn't addressed – Nic Oct 05 '16 at 22:16
  • @QPaysTaxes And what about this one? Which also got flagged to that other post. [Sort int array in descending order](http://stackoverflow.com/questions/7414299/sorting-int-array-in-descending-order) – OneCricketeer Oct 05 '16 at 22:19
  • @cricket_007 That one is explicitly asking "How do I sort integers?". This one is asking "I have code that is supposed to perform some task, but I'm getting a compiler error. Why?" The fact that the task _happens_ to be sorting integers doesn't mean a thing. Again, look at the answers of your proposed duplicate. Do they answer the question asked -- namely, "Where is the problem?" No. They give a(n alternate) solution for the task at hand, which is useful, but not an answer to the question. – Nic Oct 05 '16 at 22:21

4 Answers4

2

There is no method that takes a primitive array like int[] nums and sorts it in descending order. There are some that take an array of Objects, like sort(T[] a, Comparator<? super T> c) - but primitives are not Objects1.

The most straightforward method is likely to simply sort(int[] input) your array in ascending order, then reverse the resulting array. The sort is likely to take significantly longer than the reverse, so this method should perform well. Alternately, you may be able to modify the consuming code so that it deals with the array in ascending order, or you could wrap the array in a List and then use a reversed view.

There are lots of other options as well.


1 You could, in principle, convert your int[] into an Integer[], by boxing each underlying int element, but you would pay a large performance penalty and a huge memory (increasing the memory used by about 10x) and garbage penalty to do so.

Community
  • 1
  • 1
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
1

Your array is of type int and Comparator generic type is Integer. Change your array type to Integer and everything will be fine, like this:

public void nextPermutation(Integer[] nums) {...

Shadov
  • 5,421
  • 2
  • 19
  • 38
0

Your nums array must be of type Integer.

midwestcode
  • 23
  • 2
  • 7
0

The Arrays.sort overload that takes a Comparator can only work on arrays of Objects; int isn't a valid substitution for a generic type argument.

So,

Integer[] intObjs = ...;
int[] intPrimitives = ...;
Arrays.sort(intObjs, start, end, someComparator); //OK
Arrays.sort(intPrimitives, start, end, someComparator); //fails

For what you want to work, there'd have to be an Arrays.sort overload declared to take an int[] - but that'd be a costly sort, since it'd basically require boxing the ints for each comparison.

Sbodd
  • 11,279
  • 6
  • 41
  • 42