From: Phrogz Date: 2007-07-27T06:40:00+09:00 Subject: Re: List of directory. On Jul 26, 4:54 am, Marcin Tyman wrote: > Does anybody know how to list directory in alphabetical order? i.e. > I have following files: > a10 > a1 > a2 > a20 > > sort method sorts them as follows: > > a1 > a10 > a2 > a20 > > but I need such order: > > a1 > a2 > a10 > a20 files = %w| a1 ab1a ab1b ac1 a2 ab2 ab3 b1 bb1 ba1 a10 a20 a10a a aa ab ac | puts files.sort_by{ |f| f.split( /(\d+)/ ).map{ |piece| Integer(piece) rescue piece } } #=> a #=> a1 #=> a2 #=> a10 #=> a10a #=> a20 #=> aa #=> ab #=> ab1a #=> ab1b #=> ab2 #=> ab3 #=> ac #=> ac1 #=> b1 #=> ba1 #=> bb1