How to sort the digits of a number in descending order with C#

1 Answer

0 votes
using System;
using System.Linq;
 
class Program
{
    static void Main() {
        int n = 81925;
          
        try {
            string s = n.ToString();
            s = String.Concat(s.OrderByDescending(ch => ch));
            n = Int32.Parse(s);
            Console.WriteLine(n);
        }
        catch (FormatException e) {
            Console.WriteLine(e.Message);
        }
    }
}
 
 
 
/*
run:
 
98521
 
*/

 



answered Mar 27, 2021 by avibootz

Related questions

1 answer 115 views
1 answer 114 views
1 answer 110 views
3 answers 196 views
1 answer 135 views
...