Ale*_*lex 2 ruby arrays activerecord ruby-on-rails object
让o1, o2, o3, o4activerecord对象使用
o1.kind = "att2"o2.kind = "att3"o3.kind = "att4"o4.kind = "att1"让 a = [o1, o2, o3, o4]
让 b = ['att1', 'att3', 'att4', 'att2']
我需要排序a,b以便新订单a成为:
a = [o4, o2, o3, o1]
Run Code Online (Sandbox Code Playgroud)
我试过了
a.sort_by do |element|
b.index(element)
end
Run Code Online (Sandbox Code Playgroud)
但是如何排序 kind?
你需要索引element.kind,而不是element.
a.sort_by do |element|
b.index(element.kind)
end
Run Code Online (Sandbox Code Playgroud)