From: Park Heesob Date: 2002-08-07T09:22:54+09:00 Subject: Re: sort problem Hi, "Ian Macdonald" wrote in message news:20020806235442.GA17078@caliban.org... > Hi, > > I have a file with data like this: > > ina1 > ina15 > inb12 > ina9 > inc10 > inda12 > inef14 > inab14 > inzz8 > inzz80 > inzz9 > > etc. > > I want to sort it alphabetically on the first part of the string, then > numerically on the second part. > .... > > > Ruby > ---- > > $/ = nil > foo = gets.split("\n") > r = /^([a-z]+)([0-9]+)$/ > 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 > > > > So, what's wrong with the Ruby code? This is Ruby 1.6.7, in case that > matters. > Try this: $/ = nil foo = gets.split("\n") r = /^([a-z]+)([0-9]+)$/ foo = foo.sort {|a,b| c,d = r.match(a)[1,2] e,f = r.match(b)[1,2] (c <=> e).nonzero? || d.to_i <=> f.to_i} puts foo > Ian > -- > Ian Macdonald | Somewhere in Tenafly, New Jersey, a > ian@caliban.org | chiropractor is viewing "Leave it to > | Beaver"! > | > | > Park Heesob