From: Matthew Diephouse Date: 2002-06-04T02:18:23+09:00 Subject: Re: ruby equivalent for perl multi-index sort? Rick Bradley wrote: > Coming from a perl background I have a lot of programs which do > operations on arrays, including sorts. For example today I came across > one of mine where I wrote: > > @filelist = > map { $_->[0] . ($_->[1] ? '.'.$_->[1] : '') } > sort {$a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } > map { /(\d+)(?:\.(\d+))?/; [ $1, $2 || 0 ] } > grep {/^\d+(?:\.\d+)?$/} > readdir(INDIR); > > It takes a list of files, extracts the numeric ones and orders them > numerically. If they have fractional parts it numbers them by treating > the fractional parts as integers (don't ask why ;-). That code seems redundant, because you're doing a seperate sort on the fractions when you could instead use just one sort. Consider the following functionally equivalent (and a lot more efficient) perl: @filelist = sort { $a <=> $b } grep {/^\d+(?:\.\d+)?$/} readdir(INDIR); That would also making translating it to ruby a lot easier. md |- m:att d:iephouse