From: Florian Frank Date: 2004-05-04T04:15:10+09:00 Subject: Re: ruby 1.8 differences? On 2004-05-03 20:23:41 +0900, Jean-Hugues ROBERT wrote: > I started refactoring my code to use it. But I use a lot of sort!() > and sort_by! does not exists :-( > > Question : Why no sort_by!() ? sort_by is a more efficent way of Schwartzian Transform (because it's implemented in C). So instead of ary.map {|x| [f(x), x] }.sort {|(ka,),(kb,)| ka <=> kb }.map { |k,v| v } you can just write: ary.sort_by { |x| f(x) } Temporary arrays are created anyway. That's why it is not useful to have sort_by!. In general Schwartzian Transform is a stupid idea, especially for large arrays, but sometimes it's possible to find a function f with an image that can be used for comparing the values more efficiently than the values themselves. Here's a perl-centered paper about a lot of sorting tricks, but most of it is also true for Ruby: "A Fresh Look at Efficient Perl Sorting", http://www.sysarch.com/perl/sort_paper.html -- c, s, x = gets, c[0, 2].to_i, "dsfd;kfoA,.iyewrkldJKDHSUB" puts (1...(c.size / 2)).inject("") do |p, i| p << (c[2 * i, 2].to_i(16) ^ x[(s += 1) && s - 1]) end