我试图根据单词按字母顺序出现的次数对文档进行排序,因此当输出时,它看起来像这样.
Unsorted:
'the', '6'
'we', '7'
'those', '5'
'have', '3'
Sorted:
'we', '7'
'the', '6'
'those', '5'
'have', '3'
Run Code Online (Sandbox Code Playgroud)
Mih*_*der 10
试试这个:
假设:
a = {
'the' => '6',
'we' => '7',
'those' => '5',
'have' => '3',
'hav' => '3',
'haven' => '3'
}
Run Code Online (Sandbox Code Playgroud)
然后这样做:
b = a.sort_by { |x, y| [ -Integer(y), x ] }
Run Code Online (Sandbox Code Playgroud)
b 将如下所示:
[
["we", "7"],
["the", "6"],
["those", "5"],
["hav", "3"],
["have", "3"],
["haven", "3"]
]
Run Code Online (Sandbox Code Playgroud)
编辑按反向频率排序.
| 归档时间: |
|
| 查看次数: |
2438 次 |
| 最近记录: |