This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Sorting an Array by Frequency of Appearance
CodeExpectedActual
module Enumerable
  def sort_by_frequency
    histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash}
    sort_by { |x| [histogram[x], x] }
  end
end
[1,2,3,4,1,2,4,8,1,4,9,16].sort_by_frequency
[3, 8, 9, 16, 2, 2, 1, 1, 1, 4, 4, 4] [3, 8, 9, 16, 2, 2, 1, 1, 1, 4, 4, 4]
[1,2] <=> [0,2]
1 1
[1,2] <=> [1,2]
0 0
[1,2] <=> [2,2]
-1 -1
[1,2] <=> [1,1]
1 1
[1,2] <=> [1,3]
-1 -1
[1,2] <=> [1]
1 1
[1,2] <=> [3]
-1 -1
[1,2] <=> [0,1,2]
1 1
[1,2] <=> []
1 1
module Enumerable
  def sort_by_frequency_faster
    histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash}
    sort_by { |x| histogram[x] }
  end
end
[1,2,3,4,1,2,4,8,1,4,9,16].sort_by_frequency_faster
[16, 8, 3, 9, 2, 2, 4, 1, 1, 4, 4, 1] [16, 8, 3, 9, 2, 2, 4, 1, 1, 4, 4, 1]
module Enumerable
  def sort_by_frequency_descending
    histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash}
    sort_by { |x| [histogram[x] * -1, x]}
  end
end
[1,2,3,4,1,2,4,8,1,4,9,16].sort_by_frequency_descending
[1, 1, 1, 4, 4, 4, 2, 2, 3, 8, 9, 16] [1, 1, 1, 4, 4, 4, 2, 2, 3, 8, 9, 16]
module Enumerable
  def sort_distinct_by_frequency
    histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash }
    histogram.keys.sort_by { |x| [histogram[x], x] }
  end
end
[1,2,3,4,1,2,4,8,1,4,9,16].sort_distinct_by_frequency
[3, 8, 9, 16, 2, 1, 4] [3, 8, 9, 16, 2, 1, 4]