From: Josselin Date: 2007-06-28T20:45:02+09:00 Subject: Dynamic sort_by criteria, making it DRY... I am presently using a sort_by on my array => buddies = user_buddies.sort_by {|item| item.last_seen_at} I would like to be able to do the same , based on a variable @criteria set to the item key to be used @criteria = 'last_seen_at' @criteria = 'display_name' ...... => buddies = user_buddies.sort_by {|item| item.<@criteria> } what could be the best way to do it ? Depending upon another variable @reverse_order = true or false, I would like to perform or not teh reverse! action => buddies = user_buddies.sort_by {|item| item.last_seen_at}.reverse! I believe it should be => buddies = @reverse_order ? user_buddies.sort_by {|item| item.<@criteria> } : user_buddies.sort_by {|item| item.<@criteria> }.reverse! thanks for your enlightment ! joss