Ruby sort hash of hashes

user1107201

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!

Aleksei Matiushkin
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]

編集
0

コメントを追加

0

関連記事

分類Dev

Sort array of arrays by array value into hash in ruby

分類Dev

hash() function producing inconsistent hashes

分類Dev

Sorting list of hashes in Perl with sort

分類Dev

syntax for accessing hash param in hash of hashes when using foreach

分類Dev

Ruby, unique hashes in array based on multiple fields

分類Dev

New way of creating hashes in ruby 2.2.0

分類Dev

Ruby on Rails - Gather hashes with same specific value

分類Dev

Updating Hash value in Ruby

分類Dev

ruby / hash:ActiveSupport :: HashWithIndifferentAccess

分類Dev

Append a hash to a series of nested hashes in Puppet4

分類Dev

Given a hash of arrays, how to create an array of hashes with each possible combo

分類Dev

How to convert array of hashes into a single hash and count repeated items?

分類Dev

Sort array of arrays that is a value of a hash

分類Dev

Uitableview Sort with Hash Symbol in sectionIndexTitles

分類Dev

Implementing quick sort in Ruby

分類Dev

invalid key hash. the key hash does not match any stored key hashes facebook android

分類Dev

Adding javascript values into Ruby Hash

分類Dev

ruby array of hash with same value

分類Dev

access elements in Ruby nested hash

分類Dev

Using an array as a hash key in Ruby

分類Dev

How to remove outer hash in Ruby

分類Dev

How to merge two hashes that have same keys in ruby

分類Dev

Can Ruby hashes be included in Sass and CoffeeScript, allowing data to be shared?

分類Dev

Ruby - Extract value of a particular key from array of hashes

分類Dev

Is there a way of changing the key of a hash via sort / sort_by?

分類Dev

Is there a ruby or rails method that will return the hash key when checking for the hash key?

分類Dev

Convert hash params into instance variables on Ruby initializer

分類Dev

Need to refactor to the new Ruby 1.9 hash syntax

分類Dev

Is there a way to match the elements of an array to the keys of a hash in Ruby?

Related 関連記事

  1. 1

    Sort array of arrays by array value into hash in ruby

  2. 2

    hash() function producing inconsistent hashes

  3. 3

    Sorting list of hashes in Perl with sort

  4. 4

    syntax for accessing hash param in hash of hashes when using foreach

  5. 5

    Ruby, unique hashes in array based on multiple fields

  6. 6

    New way of creating hashes in ruby 2.2.0

  7. 7

    Ruby on Rails - Gather hashes with same specific value

  8. 8

    Updating Hash value in Ruby

  9. 9

    ruby / hash:ActiveSupport :: HashWithIndifferentAccess

  10. 10

    Append a hash to a series of nested hashes in Puppet4

  11. 11

    Given a hash of arrays, how to create an array of hashes with each possible combo

  12. 12

    How to convert array of hashes into a single hash and count repeated items?

  13. 13

    Sort array of arrays that is a value of a hash

  14. 14

    Uitableview Sort with Hash Symbol in sectionIndexTitles

  15. 15

    Implementing quick sort in Ruby

  16. 16

    invalid key hash. the key hash does not match any stored key hashes facebook android

  17. 17

    Adding javascript values into Ruby Hash

  18. 18

    ruby array of hash with same value

  19. 19

    access elements in Ruby nested hash

  20. 20

    Using an array as a hash key in Ruby

  21. 21

    How to remove outer hash in Ruby

  22. 22

    How to merge two hashes that have same keys in ruby

  23. 23

    Can Ruby hashes be included in Sass and CoffeeScript, allowing data to be shared?

  24. 24

    Ruby - Extract value of a particular key from array of hashes

  25. 25

    Is there a way of changing the key of a hash via sort / sort_by?

  26. 26

    Is there a ruby or rails method that will return the hash key when checking for the hash key?

  27. 27

    Convert hash params into instance variables on Ruby initializer

  28. 28

    Need to refactor to the new Ruby 1.9 hash syntax

  29. 29

    Is there a way to match the elements of an array to the keys of a hash in Ruby?

ホットタグ

アーカイブ