Created
February 3, 2012 21:52
-
-
Save nahurst/1732909 to your computer and use it in GitHub Desktop.
Order by occurrence count and list count of an array
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
days = ["Friday", "Saturday", "Sunday", "Monday", "Friday", "Saturday", "Friday"] | |
days.inject({}) {|h,day| h[day] = h.include?(day) ? h[day]+1 : 1; h }.sort_by{|k,v|v}.reverse.each{|k,v| puts [k,v].join(",")} | |
#Friday,3 | |
#Saturday,2 | |
#Sunday,1 | |
#Monday,1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment