Generation

generate functionTue, 21 Feb 2023

Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number. Examples: Input: 42145 Output: 54421 Input: 145263 Output: 654321 Input: 123456789 Output: 987654321

static int sortDescending(int num) { int result = 0; List<int> arr = num.ToString().ToArray().Select(x => int.Parse(x.ToString())).ToList(); arr.Sort(); arr.Reverse(); foreach (var i in arr) { result = result * 10 + i; } return result; }

Questions about programming?Chat with your personal AI assistant