我正在尝试根据单词按字母顺序出现的次数对文档进行排序,因此在输出时它会看起来像这样。
Unsorted:
'the', '6'
'we', '7'
'those', '5'
'have', '3'
Sorted:
'we', '7'
'the', '6'
'those', '5'
'have', '3'
答案 0 :(得分:10)
试试这个:
假设:
a = {
'the' => '6',
'we' => '7',
'those' => '5',
'have' => '3',
'hav' => '3',
'haven' => '3'
}
然后这样做:
b = a.sort_by { |x, y| [ -Integer(y), x ] }
b
将如下所示:
[
["we", "7"],
["the", "6"],
["those", "5"],
["hav", "3"],
["have", "3"],
["haven", "3"]
]
编辑按反向频率排序。
答案 1 :(得分:3)
words = {'the' => 6,'we' => 7,'those' => 5,'have' => 3}
sorted_words = words.sort { |a,b| b.last <=> a.last }
sorted_words.each { |k,v| puts "#{k} #{v}"}
产生
we 7
the 6
those 5
have 3
为了便于比较,您可能希望值为整数而不是字符串。
修改强>
糟糕,忽略了需要按键排序的要求。所以:
words = {'the' => 6,'we' => 7,'those' => 5,'have' => 3,'zoo' => 3,'foo' => 3}
sorted_words = words.sort do |a,b|
a.last == b.last ? a.first <=> b.first : b.last <=> a.last
end
sorted_words.each { |k,v| puts "#{k} #{v}"}
产生
we 7
the 6
those 5
foo 3
have 3
zoo 3
答案 2 :(得分:1)
在散列上使用sort
方法时,在比较块中会收到两个元素数组,您可以通过它们在一次传递中进行比较。
hsh = { 'the' => '6', 'we' => '6', 'those' => '5', 'have' => '3'}
ary = hsh.sort do |a,b|
# a and b are two element arrays in the format [key,value]
value_comparison = a.last <=> b.last
if value_comparison.zero?
# compare keys if values are equal
a.first <=> b.first
else
value_comparison
end
end
# => [['have',3],['those',5],['the',6],['we',6]]
请注意,结果是一个数组数组,因为哈希在ruby中没有内在顺序
答案 3 :(得分:1)
试试这个:
words = {'the' => 6,'we' => 7,'those' => 5,'have' => 3}
words.sort { |(x_k, x_v), (y_k, y_v)| [y_v, y_k] <=> [x_v, x_k]}
#=> [["we", 7], ["the", 6], ["those", 5], ["have", 3]]
答案 4 :(得分:1)
histogram = { 'the' => 6, 'we' => 7, 'those' => 5, 'have' => 3, 'and' => 6 }
Hash[histogram.sort_by {|word, freq| [-freq, word] }]
# {
# 'we' => 7,
# 'and' => 6,
# 'the' => 6,
# 'those' => 5,
# 'have' => 3
# }
注意:这假定您使用数字来存储数字。在您的数据模型中,您似乎使用字符串来存储数字。我不知道你为什么要这样做,但如果你做想要这样做,你显然必须在排序之前将它们转换为数字,然后再转换回字符串。
此外,这假设Ruby 1.9。在Ruby 1.8中,哈希不是有序的,所以你不能将排序后的结果转换回哈希,因为这会丢失排序信息,你必须将它保存为数组。
答案 5 :(得分:0)
word_counts = {
'the' => 6,
'we' => 7,
'those' => 5,
'have' => 3,
'and' => 6
};
word_counts_sorted = word_counts.sort do
|a,b|
# sort on last field descending, then first field ascending if necessary
b.last <=> a.last || a.first <=> b.first
end
puts "Unsorted\n"
word_counts.each do
|word,count|
puts word + " " + count.to_s
end
puts "\n"
puts "Sorted\n"
word_counts_sorted.each do
|word,count|
puts word + " " + count.to_s
end
答案 6 :(得分:0)
1.9.1
>> words = {'the' => 6,'we' => 7, 'those' => 5, 'have' => 3}
=> {"the"=>6, "we"=>7, "those"=>5, "have"=>3}
>> words.sort_by{ |x| x.last }.reverse
=> [["we", 7], ["the", 6], ["those", 5], ["have", 3]]