I have an array of similar objects which I need to sort based on one their many attributes. For instance, Campaign has clicks, impressions and cost. I want to be able to dynamically sort by any of those attributes.
def sort_campaign_by(attr) @campaigns.sort_by{ |c| c.#{attr} } end
this (obviously) doesn’t work but hopefully my intentions are clear. I’m trying not to use a case statement - seems unnecessarily lengthy and brittle.
note, I can’t sort them in the AR call, it needs to happen afterwards.
Anyone know how to do this?
thanks, ed