From: Grehom Date: 2006-01-05T00:32:58+09:00 Subject: Re: Newbie question about nested sort Thanks everyone, very interesting. The sort_by method certainly will earn it's keep in my application as my test results show below require 'benchmark' include Benchmark alphabet = ("abcdefghij") char_freq = (1..100000).map {[alphabet[rand(9)].chr, rand(8)+1]} bm(10) do |b| b.report("Sort by") { char_freq.sort_by {|a| [-a[1], a[0]] } } b.report("Sort") { char_freq.sort {|a, b| (b[1]<=>a[1]).nonzero? || a[0]<=>b[0] } } end alphabet = ("abcdef") char_freq = (1..100000).map {[alphabet[rand(5)].chr, rand(4)+1]} bm(10) do |b| b.report("Sort by") { char_freq.sort_by {|a| [-a[1], a[0]] } } b.report("Sort") { char_freq.sort {|a, b| (b[1]<=>a[1]).nonzero? || a[0]<=>b[0] } } end produces >> C:\rubysrcs\Yahtzee>ruby -w test3.rb user system total real Sort by 1.469000 0.000000 1.469000 ( 1.469000) Sort 2.703000 0.000000 2.703000 ( 2.703000) user system total real Sort by 0.766000 0.000000 0.766000 ( 0.766000) Sort 2.125000 0.016000 2.141000 ( 2.141000)