On Thu, 7 Aug 2003 03:43, Mark J. Reed wrote:
> On Thu, Aug 07, 2003 at 01:35:40AM +0900, Brett H. Williams wrote:
> > On Aug  6, Harry Ohlsen wrote:
> > >   sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] }
> >
> > I really like this.  But is there a simple easy way to get a descending
> > sort here without reverting to sorted = people.sort { ... } ?
>
> How about
>
>  sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary]
> }.reverse
>
>
> I haven't looked, but I suspect you can't selectively make individual
> criteria ascending/descending.  Now, if we had a #stable_sort_by,
> then we could chain them together to get the desired
> results without crafting complex comparisons . . . :)

For any numeric fields, you could just use -x, rather than x. So, to sort as 
above, but in descending salary ...

   sorted = people.sort_by { |p| [p.last_name, p.first_name, -p.salary] }

but I can't think of a way to get it to work for strings, for example. Maybe 
if strings had a unary minus that changed every character to the character 
with code (255 - ascii-code) ... but that sounds like a real hack :-).  Of 
course, it's not general enough, either.