How To Sort A String Alphabetically In Ruby

Posted by Weston Ganger

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:

Article Topic:Software Development - Rails

Date:January 23, 2016

Recommended Posts

Contact

604-309-8713
info@solidfoundationwebdev.com

Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected