-2

can anyone help me with this, please?

I have an input string

Console.WriteLine("Enter some numbers");
string numbers = Console.ReadLine();

And let's say the input will be "3145". How can I ascending sort them (with loops if possible) and the output be "1345"?

I'm a very beginner...

M16
  • 1
  • 4
  • 3
    does [this](https://stackoverflow.com/questions/6441583/is-there-a-simple-way-that-i-can-sort-characters-in-a-string-in-alphabetical-ord) answer your question? – styx Feb 10 '20 at 09:27
  • 1
    `string result = string.Concat(numbers.OrderBy(c => c));` – Dmitry Bychenko Feb 10 '20 at 09:28
  • 2
    Welcome to Stack Overflow. Read [ask] and try searching. – CodeCaster Feb 10 '20 at 09:29
  • If you need to use for loop, do the following: `numbers.ToCharArray()`, then you have an array of 'chars'. Then check this out: https://stackoverflow.com/questions/239103/convert-char-to-int-in-c-sharp – G.Dimov Feb 10 '20 at 09:31
  • @styx I actually need to do this with loops, somehow...but thank you tho :) – M16 Feb 10 '20 at 09:32
  • " (with loops if possible)" welcome to StackOverflow. If you present a general problem, then it is likely that we find a general duplicate that answers this general problem. If you post your personal attempt to solve this problem with a loop and tell us what your difficulties are with the loop, then we will try to address your personal issue and answer your personal problem instead of closing this general question with a duplicate. Think about it. Please post your personal attempt to solve it using loops – Mong Zhu Feb 10 '20 at 09:33
  • @M16 why do u need to use loops? – styx Feb 10 '20 at 09:33
  • 1
    @styx because it is very likely a homework – Mong Zhu Feb 10 '20 at 09:33
  • 1
    I voted to reopen, I think the given answer doesn't answer to his question. – Marco Salerno Feb 10 '20 at 09:34
  • @MarcoSalerno "I think the given answer doesn't answer to his question" but it does. The expected result matches the one that the accepted answer in the duplicate will provide. OP has to edit the post, add the loop solving attempt, and make the problem description more individual by this – Mong Zhu Feb 10 '20 at 09:37
  • @MongZhu yes, that's right – M16 Feb 10 '20 at 10:20

1 Answers1

0

This is a possible solution:

Console.WriteLine("Enter some numbers");
string numbers = Console.ReadLine();
string result = string.Concat(numbers.Select(x => int.Parse(x.ToString())).OrderBy(x => x));
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32