"Brian Candler" <B.Candler / pobox.com> schrieb im Newsbeitrag
news:20041004085228.GA98486 / uk.tiscali.com...
> On Mon, Oct 04, 2004 at 05:17:09PM +0900, nkb wrote:
> > I've a table with about 10 fields. I would like to be able to sort
> > ascending or decendingly, based on any one of the fields. I was
> > wondering if there might an elegant way to do this in Ruby? In C, I do
> > it by keeping track of the index of the array and then assigning them
> > individually. Thanks!!!
>
> Something like this?
>
>    a = [
>      [5,3,5,"foo"],
>      [2,1,9,"bar"],
>      [3,9,3,"baz"],
>    ]
>    a.sort { |x,y| x[3] <=> y[3] }    # sort on 4th element
>
> You can get a reverse sort by
>
>    a.sort { |x,y| y[3] <=> x[3] }

That's even better:

a.sort_by {|row| row[3]}

Regards

    robert