sorting

By hawker on Aug 24, 2005

How to sort an array of strings.

# Sort array of strings.  Strings can be enclosed by
# single or double quotes.
puts ['foo', 'bar', "apple", "jack"].sort

puts "\nNow let's sort in descending order.\n\n"

# This time we'll use an easier way to make an array of words.
puts %w(foo bar apple jack).sort{ |a,b| b<=>a }

puts "\nSorted on last character of each word:\n\n"

puts %w(foo bar apple jack).sort_by{ |x| x[-1] }

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.