I have a set of activities that I want to sort based on an average rating, and how many times the activity has been rated.
activities = {
:one => { :avg_rating => 5, :total_ratings => 23 },
:two => { :avg_rating => 5, :total_ratings => 18 },
:three => { :avg_rating => 5, :total_ratings => 54 }
}
EDIT - updated so the results I was expecting was correct
The result of the sort would be in order of :three, :one, :two
Thanks!
activities.sort_by { |k,v|
[v[:avg_rating], v[:total_ratings]]
}.reverse
Whether you need the element names only:
activities.sort_by { |k,v|
[v[:avg_rating], v[:total_ratings]]
}.reverse.map &:first
#⇒ [
# [0] :three,
# [1] :one,
# [2] :two
#]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加