On 08.06.2009 17:07, andrea wrote: > Robert Klemme <shortcutter / googlemail.com> wrote: > >> Another good alternative is to use the block form of #sort: >> >> irb(main):023:0> a.sort do |x,y| >> irb(main):024:1* case >> irb(main):025:2* when x == 0 then 1 >> irb(main):026:2> when y == 0 then -1 >> irb(main):027:2> else x <=> y >> irb(main):028:2> end >> irb(main):029:1> end >> => [1, 2, 4, 8, 9, 0] > > isn't this simplified version fine too? > > irb(main):003:0> a=[0,3,2,-5,0,4] > => [0, 3, 2, -5, 0, 4] > irb(main):004:0> a.sort{|x,y| x.zero? ? 1 : x<=>y} > => [-5, 2, 3, 4, 0, 0] irb(main):003:0> a=[1,0] => [1, 0] irb(main):004:0> a.sort{|x,y| x.zero? ? 1 : x<=>y} => [0, 1] irb(main):005:0> Not what the OP wanted. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/