From: Jacob Fugal Date: 2005-12-22T06:14:39+09:00 Subject: Re: move to front of array On 12/21/05, Dan Diebolt wrote: > Try this: > > array1 = %w(a B c d Cool e f G) > array1.partition {|i| i =~ /cool/i} > array2=array1.partition {|i| i =~ /cool/i} > array2.flatten #or array2.flatten! So something like: array = %w(a B c d Cool e f G) array = array.partition{ |el| el =~ /cool/i }.flatten That certainly would work (assuming that only one element matches the condition), and has a certain elegance. I wonder if there's a significant performance penalty? It also gave me this idea: class Array def bubble_up score = lambda{ |a| (yield a) ? -1 : 1 } self.sort_by { |a,b| score[a] <=> score[b] } end def bubble_up!( &proc ) self.replace(self.bubble_up( &proc )) end end array = array.bubble_up! { |el| el =~ /cool/i } Jacob Fugal