From: Rob Biedenharn Date: 2008-10-31T10:23:17+09:00 Subject: Re: how to sort this array On Oct 30, 2008, at 6:34 PM, Simon Krahnke wrote: > * Li Chen (22:14) schrieb: > >> files=[ >> "c:/ruby/self/2004/20.txt", >> "c:/ruby/self/2004/3.txt", >> "c:/ruby/self/2004/2.txt", >> "c:/ruby/self/2004/10.txt", >> "c:/ruby/self/2004/1.txt" >> ] >> >> expected results: >> [ >> "c:/ruby/self/2004/1.txt", >> "c:/ruby/self/2004/2.txt", >> "c:/ruby/self/2004/3.txt", >> "c:/ruby/self/2004/10.txt", >> "c:/ruby/self/2004/20.txt", >> ] > > files.sort_by { | fn | fn.match(/(\d+)\.txt$/)[1].to_i } > > mfg, simon .... hth Since #to_i will stop at a non-digit, you could get simpler: files.sort_by {|fn| fn[%r{.*/([^/]+)},1].to_i } I tend to use %r{ } when the Regexp deals with / characters. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com