import java.util.*;
    
/**
 * 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.
 * 
 * Tags: Sort
 */
class LargestNum {
    public static void main(String[] args) {
        LargestNum ln = new LargestNum();
        int[] num = {3, 30, 34, 5, 9};
        System.out.println(ln.largestNumber(num));
    }
    
    /**
     * Create a comparator for sorting
     * Convert num to String and compare the concatenated result of them
     * Note {0, 0} is a special case
     */
    public String largestNumber(int[] num) {
        StringBuilder res = new StringBuilder();
        if (num == null || num.length == 0) return res.toString();
        String[] str = new String[num.length];
        for (int i = 0; i < num.length; i++) str[i] = num[i] + "";
        Comparator<String> comp = new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return Long.compare(Long.valueOf(s2 + s1), Long.valueOf(s1 + s2));
            }
        };
        Arrays.sort(str, comp); // acsending order regarding to comp, which prefers the -1 output of comp.
        /*
         * str[0] is the smallest element in order that defined by comp.
         * It implies that  for any i, comp.compare(str[0], str[i]) ==  -1,
         * So str[i] + str[0] is less than str[0] + str[i].
         */
        if (str[0].equals("0")) return "0"; // deal with 0
        for (String s : str) res.append(s);
        return res.toString();
    }
}

import java.util.*;
    
/**
 * 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.
 * 
 * Tags: Sort
 */
class LargestNum {
    public static void main(String[] args) {
        LargestNum ln = new LargestNum();
        int[] num = {3, 30, 34, 5, 9};
        System.out.println(ln.largestNumber(num));
    }
    
    /**
     * Create a comparator for sorting
     * Convert num to String and compare the concatenated result of them
     * Note {0, 0} is a special case
     */
    public String largestNumber(int[] num) {
        StringBuilder res = new StringBuilder();
        if (num == null || num.length == 0) return res.toString();
        String[] str = new String[num.length];
        for (int i = 0; i < num.length; i++) str[i] = num[i] + "";
        Comparator<String> comp = new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return Long.compare(Long.valueOf(s2 + s1), Long.valueOf(s1 + s2));
            }
        };
        
        /* Bubble sort xxs1s2xxx or xxs2s1xxx
         * compare("3","30") = -1 given 303 < 330
         * compare("5","9') = 1 given 95 > 59
         */
        Arrays.sort(str, comp); // acsending order regarding to comp, which prefers the -1 output of comp.

        if (str[0].equals("0")) return "0"; // deal with 0
        for (String s : str) res.append(s);
        return res.toString();
    }
}

Add comments on the comparator.