【发布时间】:2009-07-28 14:52:23
【问题描述】:
我有一个这样的数组:
irb(main):028:0> device_array
=> [["name1", "type1", ["A", "N", "N"], ["Attribute", "device_attribute"], 9], ["name2","type2", ["A", "N", "N"], ["Attribute", "device_attribute"], 7]]
我想在第 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
我也试过了:
AllDevicesController.all_devices.sort do | a,b |
a[][4] <=> b[][4]
end
这两种方法都没有奏效。
我以此作为参考: http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
我想我错过了一些使这变得非常容易的红宝石。
【问题讨论】: