Possible Duplicate:
How to sort a string's characters alphabetically?
Assuming there is a simple string str = "bacd" I want to sort it so that the result contains result = "abcd"
How do I do this in Ruby?
Tokland's comment is so nice it deserves to be a full-fledged answer:
str.chars.sort.join
This works in Ruby 1.8 and 1.9.
You can find the Ruby Doc here.
Nice and simple:
str = 'bacd'
p str.split('').sort.join # => "abcd"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With