From: x1 Date: 2006-10-12T13:08:55+09:00 Subject: Re: Counting Frequency of Values in an Array (And Sorting by Frequency?) Ah ok.. I'm with ya. Meet "sort_by", my new friend. Thanks again :-D On 10/12/06, Daniel Sheppard wrote: > items = Hash.new(0) > ["a","a","a","b","c","c"].each do |i| > items[i] += 1 > end > items.sort_by {|key,value| -value}.each do |key, value| > puts "#{a}:#{b}" > end > > Slightly less horrific? > > > and.. so you're aware.. my hacky code was something like this: > > > > items = {} > > ["a", "a", "a", "b", "c", "c"].each do |i| > > if items.include? i > > items[i] += 1 > > else > > items[i] = 1 > > end > > end > > > > items.sort {|a,b| a[1]<=>b[1]}.reverse.each do |a, b| > > puts a + ":" + b.to_s > > end > > > > > > horrific eh? > > >