09 - sorting an array by frequency of appearance.rb
来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 51 行
RB
51 行
module Enumerable def sort_by_frequency histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash} sort_by { |x| [histogram[x], x] } endend[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]#---[1,2] <=> [0,2] # => 1[1,2] <=> [1,2] # => 0[1,2] <=> [2,2] # => -1[1,2] <=> [1,1] # => 1[1,2] <=> [1,3] # => -1[1,2] <=> [1] # => 1[1,2] <=> [3] # => -1[1,2] <=> [0,1,2] # => 1[1,2] <=> [] # => 1#---module Enumerable def sort_by_frequency_faster histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash} sort_by { |x| histogram[x] } endend[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]#---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]} endend[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]#---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] } endend[1,2,3,4,1,2,4,8,1,4,9,16].sort_distinct_by_frequency# => [3, 8, 9, 16, 2, 1, 4]#---
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?