我有一个像这样的数组:
irb(main):028:0> device_array
=> [["name1", "type1", ["A", "N", "N"], ["Attribute", "device_attribute"], 9], ["name2","type2", ["A", "N", "N"], ["Attribute", "device_attribute"], 7]]
Run Code Online (Sandbox Code Playgroud)
我想在第4个元素上对整个device_array进行排序.
我试过了
AllDevicesController.all_devices.sort do | a,b |
for i in 0..(AllDevicesController.all_devices.length - 1) do
a[i][4] <=> b[i][4]
end
end
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
AllDevicesController.all_devices.sort do | a,b |
a[][4] <=> b[][4]
end
Run Code Online (Sandbox Code Playgroud)
这两种方法都没有奏效.
我使用它作为参考:http: //ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
我想我错过了一些红宝石,这让我很容易.