From: Garthy D Date: 2012-01-19T09:58:46+09:00 Subject: Elegant expression of sorting Hi all, I've found that for the current project I am working on that I am frequently doing sorts based on multiple keys. The comparisons on each key tend to vary, ie. sometimes I want descending rather than ascending, and other times the comparison itself is complex. I usually do something like this: foo.sort! {|a,b| # Ascending, primary. rv = a.key0 <=> b.key0 # Descending, secondary. rv = b.key1 <=> a.key1 if rv == 0 # A tricky and expensive comparison, tertiary. rv = a.magic(b) if rv == 0 rv } Does anyone have any suggestions as to a more elegant way to express this? I'm after something that is logically equivalent, but easier to write and clearer to read. Garth