I am trying to sort about 20 hashes in ruby based on one of the attributes of the array. I want to only return the top 3 hash keys, but I don't want to have to compare each one.
我试图根据数组的一个属性在ruby中排序大约20个哈希值。我想只返回前3个哈希键,但我不想比较每个哈希键。
Here is an example below of something similar to what I am trying to sort. I want to sort based on powerrank.
下面是一个类似我想要排序的例子。我想根据powerrank进行排序。
["green", {:price=>24.88, :numreviews=>822, :avgStarsRank=>41.0, :reviewsrank=>28, :powerrank=>73.976}]
["steve", {:price=>14.96, :numreviews=>3, :avgStarsRank=>40.0, :reviewsrank=>0, :powerrank=>42.992000000000004}]
["joey", {:price=>40.27, :numreviews=>814, :avgStarsRank=>44.0, :reviewsrank=>28, :powerrank=>80.054}]
["board", {:price=>14.96, :numreviews=>3, :avgStarsRank=>40.0, :reviewsrank=>0, :powerrank=>47}]
["john", {:price=>40.27, :numreviews=>814, :avgStarsRank=>44.0, :reviewsrank=>28, :powerrank=>16}]
For this, all I would want back would an array with ["joey", "green", "board"]. Any suggestions on how to tackle this issue?
对于这个,我想要的只是一个带有[“joey”,“green”,“board”]的数组。关于如何解决这个问题的任何建议?
Edit: Here is the example in the original hash form:
编辑:以下是原始哈希表单中的示例:
powerrankings =
{
"green"=>{:price=>24.88, :numreviews=>822, :avgStarsRank=>41.0, :reviewsrank=>28, :powerrank=>73.976},
"steve"=>{:price=>14.96, :numreviews=>3, :avgStarsRank=>40.0, :reviewsrank=>0, :powerrank=>42.992000000000004},
"joey"=>{:price=>40.27, :numreviews=>814, :avgStarsRank=>44.0, :reviewsrank=>28, :powerrank=>80.054}}
"board"=>{:price=>14.96, :numreviews=>3, :avgStarsRank=>40.0, :reviewsrank=>0, :powerrank=>47},
"john"=>{:price=>40.27, :numreviews=>814, :avgStarsRank=>44.0, :reviewsrank=>28, :powerrank=>16}
}
1 个解决方案
#1
3
Assuming that powerrankings
is not as you showed but is rather,
假设powerrankings不是你所展示的,而是相反,
powerrankings = {
"green" => {:price=>24.88, :numreviews=>822, :avgStarsRank=>41.0, :reviewsrank=>28, :powerrank=>73.976},
"steve" => {:price=>14.96, :numreviews=>3, :avgStarsRank=>40.0, :reviewsrank=>0, :powerrank=>42.992000000000004},
"joey" => {:price=>40.27, :numreviews=>814, :avgStarsRank=>44.0, :reviewsrank=>28, :powerrank=>80.054},
"board" => {:price=>14.96, :numreviews=>3, :avgStarsRank=>40.0, :reviewsrank=>0, :powerrank=>47},
"john" => {:price=>40.27, :numreviews=>814, :avgStarsRank=>44.0, :reviewsrank=>28, :powerrank=>16}
}
then,
然后,
powerrankings.sort_by{|_, h| h[:powerrank]}.reverse.first(3).map(&:first)
# => ["joey", "green", "board"]
#1
3
Assuming that powerrankings
is not as you showed but is rather,
假设powerrankings不是你所展示的,而是相反,
powerrankings = {
"green" => {:price=>24.88, :numreviews=>822, :avgStarsRank=>41.0, :reviewsrank=>28, :powerrank=>73.976},
"steve" => {:price=>14.96, :numreviews=>3, :avgStarsRank=>40.0, :reviewsrank=>0, :powerrank=>42.992000000000004},
"joey" => {:price=>40.27, :numreviews=>814, :avgStarsRank=>44.0, :reviewsrank=>28, :powerrank=>80.054},
"board" => {:price=>14.96, :numreviews=>3, :avgStarsRank=>40.0, :reviewsrank=>0, :powerrank=>47},
"john" => {:price=>40.27, :numreviews=>814, :avgStarsRank=>44.0, :reviewsrank=>28, :powerrank=>16}
}
then,
然后,
powerrankings.sort_by{|_, h| h[:powerrank]}.reverse.first(3).map(&:first)
# => ["joey", "green", "board"]