按键排序Ruby哈希,然后按值排序
我使用group_by返回一个哈希,我想先按键排序,然后按该键下的值排序 这是我到目前为止想出的代码。一切都正常,只是我不知道如何在分组后对值进行排序 我有一个方法,'pp_accounts_with_properties',它显示了我的意图并以正确的顺序打印出所有内容(感谢properties.sort_by!call)。。。但似乎我应该能够通过“accounts\u with\u properties”方法以某种方式实现这一点按键排序Ruby哈希,然后按值排序,ruby,ruby-on-rails-3,Ruby,Ruby On Rails 3,我使用group_by返回一个哈希,我想先按键排序,然后按该键下的值排序 这是我到目前为止想出的代码。一切都正常,只是我不知道如何在分组后对值进行排序 我有一个方法,'pp_accounts_with_properties',它显示了我的意图并以正确的顺序打印出所有内容(感谢properties.sort_by!call)。。。但似乎我应该能够通过“accounts\u with\u properties”方法以某种方式实现这一点 class ProofList attr_reader :c
class ProofList
attr_reader :client
def initialize(client)
@client = client
end
def properties
@client.properties.joins(invoices: [charges: [:charge_credits]])
.where(charge_credits: { approved: false }).uniq
end
def accounts_with_properties
unsorted_accounts_with_properties.sort_by { |account, properties| account[:acct_number] }
end
def unsorted_accounts_with_properties
properties.group_by { |property| property.account }
end
def pp_accounts_with_properties
accounts_with_properties.each do |account, properties|
puts account.name
properties.sort_by! { |p| p[:name] }
properties.each do |property|
puts property.name
end
end
end
end
你是
查看这篇文章,可以得到比我更清晰的解释:我只是为了显示而进行排序。但是我必须在多个地方做,所以我想如果我能在生成它的时候做,那么我就可以避免每次显示数据时都对它进行排序。。。但是是的,我明白你的意思,试着用命令来做任何操作,操纵数据。