Search code examples
c#arraysstringbuilder

Make the largest number of the number


i try to make the largest number, of entered number. Have some ideas, but all of them end by error.

int n = 123 // in the end shell be 321, 567 -> 765, 184 -> 841
StringBuilder str = new StringBuilder(n.ToString());
            int[] aray = new int[3];
            for (int i = 0;i < str.Length;i++) {
                aray[i] = int.Parse(str[i].ToString());
            }
            aray.OrderBy(x => x);
            var a = aray.Join(' '); //eror
            Console.WriteLine(a);

Solution

  • You have a lot of code to accomplish something very simple:

    var ans = int.Parse(String.Concat(n.ToString().OrderByDescending(dc => dc)));