Sortieren eines Arrays von Arrays in Ruby
Ich habe eine Reihe von Arrays wie folgt:
irb(main):028:0> device_array
=> [["name1", "type1", ["A", "N", "N"], ["Attribute", "device_attribute"], 9], ["name2","type2", ["A", "N", "N"], ["Attribute", "device_attribute"], 7]]
Ich möchte das gesamte device_array nach dem 4. Element sortieren.
ich habe es versucht
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
Ich habe auch versucht:
AllDevicesController.all_devices.sort do | a,b |
a[][4] <=> b[][4]
end
Beide Methoden haben nicht funktioniert.
Ich habe dies als Referenz verwendet:http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
Ich stelle mir vor, ich vermisse etwas Rubinrotes, was das wirklich einfach macht.