Skip to content

Instantly share code, notes, and snippets.

@ph3nx
Created January 19, 2014 20:45
Show Gist options
  • Save ph3nx/8510781 to your computer and use it in GitHub Desktop.
Save ph3nx/8510781 to your computer and use it in GitHub Desktop.
Some ways to sort arrays in Ruby. Copy and paste the code examples in the irb console to try them.
array = [3,7,1,9,2,12,34,0]
# sort in ascending order
array.sort
# => [0, 1, 2, 3, 7, 9, 12, 34]
# sort in descending order
array.sort.reverse
# => [34, 12, 9, 7, 3, 2, 1, 0]
array.sort { |a,b| b <=> a }
# => [34, 12, 9, 7, 3, 2, 1, 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment