Rails

How To Sort A String Alphabetically In Ruby

Posted by Weston Ganger on January 23, 2016

I needed to sort string alphabetically in ruby.

There is a handy chars method on string that converts a string to char array


#Case Sensitive
my_string.chars.sort.join

# Case Insensitive
str.chars.sort{|a,b| a.casecmp(b)}.join
# or
str.chars.sort_by(&:downcase).join


Related External Links:


Posted in Rails and Tagged with ruby sort 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts