Sorting arrays with Ruby

This simple example shows how you can manually sort arrays in Ruby, without using the sort method.

# Sorting array of Words

def sort arr
  return arr if arr.length <= 1

  middle = arr.pop
  less   = arr.select{|x| x < middle}
  more   = arr.select{|x| x >= middle}

  sort(less) + [middle] + sort(more)
end

p(sort(['can', 'feel', 'singing', 'like', 'a', 'can']))