Skip to content

Instantly share code, notes, and snippets.

@nahurst
Created February 3, 2012 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahurst/1732909 to your computer and use it in GitHub Desktop.
Save nahurst/1732909 to your computer and use it in GitHub Desktop.
Order by occurrence count and list count of an array
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