From: Mike Stephens Date: 2010-12-02T03:30:35+09:00 Subject: Re: Help sorting an array If you've ever read "Real Programmers don't use Pascal" (see http://www.codeproject.com/KB/scrapbook/realprog.aspx ) you'll know that "Determined Real Programemrs can write Fortran programs in any language" Just because you're a Ruby programmer you don't have to use all that OO stuff. This algorithm picks out the first label and moves it into a new space on the left, and then moves the second label to where the first was and so on. Lastly it removes the vacated slot on the right: def initialise_globals $m1 = "JANUARY" $m2 = "FEBRUARY" $m3 = "MARCH" $m1_index = 0 $m2_index = 0 $m3_index = 0 end def get_array $input_array = ["#", "#", "MARCH", "#", "#", "FEBRUARY", "#","JANUARY"] puts " #{$input_array}" end def shuffle_all_right #insert m3 while we’re at it. $input_array.unshift " " end def locate_months $m1_index = $input_array.index $m1 $m2_index = $input_array.index $m2 $m3_index = $input_array.index $m3 end def bring_months_to_left $input_array[0] = $input_array[$m3_index] $input_array[$m3_index] = $input_array[$m2_index] $input_array[$m2_index] = $input_array[$m1_index] end def delete_right_slot $input_array.pop end def main_program initialise_globals get_array shuffle_all_right locate_months bring_months_to_left delete_right_slot end main_program puts $input_array Actually I cheated there as I enclosed the #strings in quotes. Anyone know how to deal with #s? -- Posted via http://www.ruby-forum.com/.