Java using Custom Comparator


#1
public class Solution {
    private class LargestNumberComparator implements Comparator<String>{
        @Override
        public int compare(String a, String b){
            return (b+a).compareTo(a+b); //descending order
        }
    }
    public String largestNumber(final int[] A) {
        String[] str = new String[A.length];
        for(int i=0;i<A.length;i++)
            str[i] = String.valueOf(A[i]);
        Arrays.sort(str, new LargestNumberComparator());
        if(str[0].equals("0"))
            return "0";
        StringBuilder largestNumberString = new StringBuilder();
        for(String s:str)
            largestNumberString.append(s);
        return largestNumberString.toString();
    }
}.