Last active
February 15, 2016 19:12
-
-
Save robvolk/180307ac7f6c71aaa6a3 to your computer and use it in GitHub Desktop.
Count & sort values of a Ruby Array
This file contains hidden or 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
food = ["bacon", "eggs", "cheese", "eggs", "bacon", "bacon", "bacon"] | |
food.inject(Hash.new(0)) {|h, k| h[k] += 1; h }.sort_by {|k,v| -1 * v} | |
# => [["bacon", 4], ["eggs", 2], ["cheese", 1]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment