User inputs an integer/string. I want to sort the digits/characters of this integer/string. How can I accomplish that in Ruby? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

User inputs an integer/string. I want to sort the digits/characters of this integer/string. How can I accomplish that in Ruby?

I hope there is an easy way, but I couldn't find a method who does the work. Examples: 47983 --> result: 34789 helloworld --> result: dehllloorw

21st Sep 2017, 11:04 AM
Ben
Ben - avatar
1 Answer
0
Ok, i did a little bit of research and it seems like the following will do the trick: n = gets # String input z1 = n.chars.sort.join # String output z2 = n.chars.sort.join.to_i # Integer output or even the reversed number with: z3 = n.chars.sort.reverse.join.to_i
21st Sep 2017, 8:59 PM
Ben
Ben - avatar