From: Daniel Lucraft Date: 2007-07-19T18:14:33+09:00 Subject: Re: confused, need help with a sorting program johny.lam wrote: > I was doing the recursive sorting program in Chapter 10 of Chris > Pine's learning to program and i wrote this: > > what i don't get is that this program sorts the words into reverse > alphabetical order. > what i am trying to is move the smallest word into the sorted_array > but if i use the greater than sign wouldn't the move the biggest word > into the sorted_array? Yes, that's true. The problem here is that you are not calling the function recursively with sorted_array. The sorted_array is getting discarded at each level and replaced with [] each time, since you're calling 'sort' instead of 'recursive_sort'. There is a little illusion here, which you will see if you change the 'puts' to a 'p'. You are not actually getting an full array out the end, it just looks like you are because you are printing the elements one at a time. You need to pass the sorted_array down into the next level of recursive_sort, by changing unsorted_array.pop sort(unsorted_array) to unsorted_array.pop recursive_sort(unsorted_array, sorted_array) Now this will print out the sorted array n times, since as each recursive_sort returns, it will look at the now empty unsorted_array and then print sorted_array. I'd get rid of if unsorted_array.length==0 p sorted_array # <-- was 'puts' end altogether and arrange it so that the sort function returns the sorted array: def recursive_sort unsorted_array, sorted_array ... return sorted_array end ... # call the sort function p sort(word) best, Dan -- Posted via http://www.ruby-forum.com/.