From: GOTO Kentaro Date: 2002-08-08T01:26:29+09:00 Subject: Re: sort problem At Wed, 7 Aug 2002 14:31:47 +0900, WATANABE Hirofumi wrote: > :foo = foo.sort {|a,b| c,d = r.match(a)[1,2] > : e,f = r.match(b)[1,2] > : c <=> e or d.to_i <=> f.to_i} > :puts foo > > How about Array#<=> ? > > [c, d.to_i] <=> [e, f.to_i] And Ruby 1.7 has Array#sort_by. foo = foo.sort_by { |i| e,f = r.match(a)[1,2] [e, f.to_i] } By the way, this example reminds me. I would sometimes like to modifiy an array in a method chain. So, I wrote Array#sub_at and Array#sub. With this method, above can be rewritten as follows: foo = foo.sort_by { |i| r.match(a)[1,2].sub_at(1) { |f| f.to_i } } Or the original problem [ruby-talk:46468] can be answered as: % cat ruby-talk:46468.rb require "arraysub" puts ARGF.readlines.sort_by { |line| line.scan(/\D+|\d+/).sub(/\d/) { |f| f.to_i } } % cat ruby-talk:46468.dat ina1 ina15 inb12 ina9 inc10 inda12 inef14 inab14 inzz8 inzz80 inzz9 % ruby17 ruby-talk:46468.rb ruby-talk:46468.dat ina1 ina9 ina15 inab14 inb12 inc10 inda12 inef14 inzz8 inzz9 inzz80 I've put arraysub.rb at http://www.notwork.org/~gotoken/ruby/p/as-is/arraysub.rb -- Gotoken