C# - sort using compare a+b vs b+a (same as others)


  • 0
    public string LargestNumber(int[] nums) 
    {
        List<string> list = nums.Select(x => x.ToString()).ToList();
        list.Sort((string a, string b) => (b + a).CompareTo(a + b));
        StringBuilder sb = new StringBuilder();
        list.ForEach(x => sb.Append(x));
        return sb[0] == '0' ? "0" : sb.ToString();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.