On Wed, Mar 12, 2008 at 12:02 AM, Ma Fe <frytaz / gmail.com> wrote:
> Hi, i have array with file names
>  a = ['one.jpg', 'two.txt', 'three.pdf']
>  how can i sort it that always first value will be *.txt file ?
>
>  Thanks, fryt
>  --
>  Posted via http://www.ruby-forum.com/.
>
>

a = ['one.jpg', 'two.txt', 'three.pdf']
#assume you want reverse sort?
a.sort_by {|i| i.scan /\..*?$/}.reverse

You understand, of course, this won't give you .txt as a first hit if
you have a .zip.  It simply reverse sorts by the letters following a
dot.

If you just want .txt files, maybe use #select instead...

a.select {|i| i =~ /\..txt$/}

Or you could use #partition if you don't care about the order of other
attributes, but want to hold on to the entire array...

a.partition {|i| i =~ /\..txt$/}.flatten

Todd