Hi,

Rick Bradley <rick / rickbradley.com> writes:

:@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);

filelist = Dir.open(".").
  grep(/^\d+(?:\.\d+)?$/).
    map {|x| x =~ /^(\d+)(?:\.(\d+))?/; [ Integer($1), Integer($2) ]}.
      sort {|a, b| (a[0] <=> b[0]).nonzero? || a[1] <=> b[1]}.
        map {|x| x[0].to_s + (x[1] != 0 ? '.' + x[1].to_s : '')}

:In Ruby 0 isn't false so the comparable Ruby expression won't trigger
:the comparison on later indices.  I've been wondering if there's a way
:to efficiently implement this idiom in Ruby.
:
:Ideas?

The Schwartzian Transform:

filelist = Dir.open(".").
  grep(/^\d+(?:\.\d+)?$/).
    map {|x| x =~ /^(\d+)(?:\.(\d+))?/; [ Integer($1), Integer($2), x ]}.
      sort.
        map {|x| x.last}

You can use Enumerable#sort_by in Ruby 1.7.

filelist = Dir.open(".").
  grep(/^\d+(?:\.\d+)?$/).
    sort_by {|x| x =~ /^(\d+)(?:\.(\d+))?/; [ Integer($1), Integer($2) ]}

-- 
eban
filelist = Dir.open(".").grep(/^\d+(?:\.\d+)?$/).sort_by {|x| x.to_f}