Robert Klemme
3/12/2008 3:20:00 PM
2008/3/12, Rodrigo Bermejo <rodrigo.bermejo@ps.ge.com>:
>
> a = ['one.jpg', 'two.txt', 'three.pdf','a.txt', 'a.az' , 'a.z']
> a_new=a.sort_by do |x|
> if x =~ /\.txt$/
> 0
> else
> 1
> end
> end
> p a_new
> --> ["a.txt", "two.txt", "one.jpg", "three.pdf", "a.az", "a.z"]
>
> the sort_by block can be improved surely.
Here's one way: sorting with priorities:
irb(main):006:0> a = ['one.jpg', 'two.txt', 'three.pdf','a.txt', 'a.az' , 'a.z']
=> ["one.jpg", "two.txt", "three.pdf", "a.txt", "a.az", "a.z"]
irb(main):007:0> a.sort_by {|s| [/\.txt$/=~s ? 0 : 1, s]}
=> ["a.txt", "two.txt", "a.az", "a.z", "one.jpg", "three.pdf"]
;-)
Kind regards
robert
--
use.inject do |as, often| as.you_can - without end