tags:

views:

36

answers:

1

I have an array with 2 different types of objects. They all have similar properties, like ratings / title etc...

An example is:

array = array.sort_by { |o| [o.type1.rating] }

Sometimes the array has 2 object types type1 and type2 is there any way to sort both of them using the sort_by method?

+1  A: 

you can use some metaprogramming for this:

array = array.sort_by { |o| o.respond_to?(:type1) ? [o.type1.rating] : [o.type2.rating] }

That should do the trick.

Derek P.