Posts [ 3 ]

Topic: Sorting an array??

I have trouble sorting an array of objects where I want to sort by whatever a method of each of these objects returns. The method name is not known in advance (since the members are ActiveRecord objects) nor is the type of the value which the method returns, nor can be guaranteed that the value will not be nil.

So I have

arr=[o1, o2, o3, o4]

and I want to sort by an attribute of these objects stored in the variable attr. Sometimes attr might contain "field1", then I want to sort the array by whatever o1.field1, o2.field1 etc. returns, sometimes attr might contain another field name.

I tried to do this with

arr.sort {|a,b| eval("a.#{attr}") <=> eval("b.#{attr}") }

but this does not work if the value returned is nil or true/false.

I then wrote a longer sort block that checks all possibilities for nil etc. and only uses <=> for non-nil and non-boolean values.

Now the next problem is that sorting does not seem to preserve order for equal elements -- how can one sort by a field1 so that for objects with equal values in field1, the sorting will be done by field2?

In perl, this is easy: since sort preserves order for equal elements, I first sort by field2, then field1. This does NOT seem to work in Ruby?