no suitable method found for sort(int[],<anonymous Comparator<Integer>>)

22,178

Solution 1

The reason for this is that int and Integer are different types. int is a primitive, and Integer is its object wrapper. Comparator<T> works only with objects; it does not work with primitives.

Put ints in a container of Integers, say, an ArrayList<Integer>, and use your comparator to solve the problem.

Note that your approach may fail for very large integers, because concatenating two valid ints may produce a number too large for an int to hold. You could return (s1+s2).compareTo(s2+s1) instead for lexicographical comparison, which is identical to numeric comparison for numbers of the same length.

Solution 2

You must use Integer

    Integer[] nums = new Integer[]{3, 30, 34, 5, 9};

    public class Solution {

    public String largestNumber(Integer[] num) {
        Arrays.sort(num, new java.util.Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                String s1 = String.valueOf(a), s2 = String.valueOf(b);
                return Integer.parseInt(s1 + s2) - Integer.parseInt(s2 + s1);
            }
        });
        StringBuilder builder = new StringBuilder();
        for (int i = num.length - 1; i >= 0; i--)   builder.append(num[i]);
        return builder.toString();
    }
}
Share:
22,178
Junior
Author by

Junior

Updated on July 09, 2022

Comments

  • Junior
    Junior almost 2 years

    Algorithm question: Given a list of non negative integers, arrange them such that they form the largest number.

    For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

    Note: The result may be very large, so you need to return a string instead of an integer.

        public class Solution {
        public String largestNumber(int[] num) {
            Arrays.sort(num, new java.util.Comparator<Integer>() {
                @Override
                public int compare(Integer a, Integer b) {
                    String s1 = String.valueOf(a), s2 = String.valueOf(b);
                    return Integer.parseInt(s1 + s2) - Integer.parseInt(s2 + s1);
                }
            });
            StringBuilder builder = new StringBuilder();
            for (int i = num.length - 1; i >= 0; i--)   builder.append(num[i]);
            return builder.toString();
        }
    }
    

    Result: Line 3: error: no suitable method found for sort(int[],<anonymous Comparator<Integer>>)

    Does anyone know how to modify it? Thanks!

    Thank you for all your detail answers, I have modified my code as

    public String largestNumber(int[] num) {
        int N = num.length;
        String[] aux = new String[N];
        for (int i = 0; i < N; i++) aux[i] = String.valueOf(num[i]); // int[] -> String[]
        Arrays.sort(aux, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return (int) (Long.parseLong(a + b) - Long.parseLong(b + a));  // note the overflow of int, max + max
            }
        });
        StringBuilder builder = new StringBuilder();
        for (int i = N - 1; i >= 0; i--)    builder.append(aux[i]);
        int sub = 0;
        for ( ; sub < builder.length() - 1 && builder.charAt(sub) == '0'; sub++);
        return builder.substring(sub).toString();
    }
    

    And I am still trying to find a way to avoid using extra space.