Given the complete lack of requirements, I'm not sure if this will help, but I would use the LINQ OrderBy list and method if I did this in production code:
List<int> testList = new List<int> { 1, 5, 10, 4, 2 }; IEnumerable<int> orderedList = testList.OrderByDescending(x => x);
To sort numbers, you must clearly understand what an "array" is. For this you need some kind of collection. I would use the list again:
List<int> digits = new List<int>(); int remainingNumber = 52146729; while (remainingNumber > 0) { digits.Add(remainingNumber % 10); remainingNumber /= 10; } IEnumerable<int> orderedDigits = testList.OrderByDescending(x => x);
This works because x mod y returns the remainder of x / y. So 9% 10 returns nine, so 19% 10, etc. Then you divide by 10 to get rid of one digit, and repeat until you finish. Then I use the same order function as before. This approach definitely does not use strings or any explicit arrays (List is an array under the hood). As I said, doing this without any collection will be really ugly.
The listing of this list will be simple (if you just need to print it for output):
foreach (int i in orderedDigits) Console.Write(i);
If you need a real number, it will be a little more complicated, so I will leave it now.
Bradleydotnet
source share