Skip to content

Instantly share code, notes, and snippets.

@cblavier
Created May 25, 2011 22:37
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 cblavier/992156 to your computer and use it in GitHub Desktop.
Save cblavier/992156 to your computer and use it in GitHub Desktop.
How can i sort an array that includes similar items by number of those similar items ?
array = ["a", "b", "b", "a", "c", "b"]
occurences = array.inject(Hash.new(0)) { |h,k| h[k] += 1; h }
array.sort_by { |k| occurences[k] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment