From: Daniel Carrera Date: 2003-01-12T22:09:09+09:00 Subject: Re: sorting with the Swartzian transform Ruby never ceases to amaze me. :) I still think it's good to understand Swartzian transforms because Enumerable#sort_by won't help in the general case when you can't easily assign a number to ech object. Still, it's very cool to have that in the language. Daniel Carrera Graduate Teaching Assistant. Math Dept. University of Maryland. (301) 405-5137 On Sun, 12 Jan 2003, WATANABE Hirofumi wrote: > Hi, > > Daniel Carrera writes: > > > This email is simply for the benefit and/or reading pleasure of ruby > > hackers. In particular those without a strong Perl background. I just > > had to do a Swartzian transform in Ruby and it occurred to me that someone > > might benefit from my experience. > > Ruby 1.8 has Enumerable#sort_by. > This method is equivalent to: > > module Enumerable > def sort_by > ary = map { |i| [yield(i), i] } > ary.sort! { |a, b| a[0] <=> b[0] } > ary.map! { |i| i[1] } > end > end > > -- > eban > >