How to use Split and Sort In Ruby

Syntax:

sorted_string = string.split(”).sort.join

Example: 

In this example the string is split into an array of characters and the sort method is used to sort the array.

Ruby
# Original string
original_string = "w3wiki"

# Sort the string alphabetically  
# Using String's each_char Method 
# and Array's sort Method
sorted_string = ''
original_string.each_char { |char| sorted_string << char }
sorted_string = sorted_string.chars.sort.join('')
puts  sorted_string

Output
eeeefggkkorss

How to Sort a String Characters Alphabetically in Ruby?

This article focuses on discussing how to sort a string’s characters alphabetically in Ruby.

Similar Reads

Using Chars and Sort

Syntax:...

Using Split and Sort

Syntax:...

Using Custom Sorting Algorithm

This method implements a custom sorting algorithm to sort the characters of the string str....