Model.find_by_tag_name("#foo") returns an array with:
- 25 records with
tag_name = "#foo" - 40 records with
tag_name = "#foobar" - 15 records with
tag_name = "#foobarbaz"
I want to count the number of times each unique tag_name showed up, sort the results by that count, then eliminate duplicates. The resulting order should be:
- #foobar
- #foo
- #foobarbaz
Currently I'm doing something like this to get the uniques and a count:
counter = Hash.new(0)
uniques = results.each{|tag| counter[tag.tag_name] += 1 }.uniq{|tag| tag.tag_name }
The above makes counter => {"#foo"=>25, "#foobar"=>40, "#foobarbaz"=>15}.
Having retrieved, de-duped and counted the results, what approaches might I take to sorting the resulting array of unique tags(uniques) by the number of times they showed up?