Created
January 19, 2014 20:45
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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