Creating my own array sort method

Hello,

I'm working through Chris Pine's book, "Learn to Program"...stuck with
this in Ch. 12 --

I have to sort an array of words, e.g.

user_inputs = [bird, zoo, dog]

without using the .sort method, e.g.

user_inputs.sort

Can someone please point me in the right direction on how to perhaps
loop through all words in an array and compare them to each other using
< to find the "smallest" word in the array?

···

--
Posted via http://www.ruby-forum.com/.

I'm not sure which methods you are allowed to use, but take a look at
Enumerable#each, and try String#< to see if it works :-).

["bird", "zoo", "dog"].each {|animal| puts animal}

"bird" < "zoo" # => true

Hope this helps,

Jesus.

···

On Tue, Nov 30, 2010 at 6:57 PM, Ilya B. <ilyabe@gmail.com> wrote:

Hello,

I'm working through Chris Pine's book, "Learn to Program"...stuck with
this in Ch. 12 --

I have to sort an array of words, e.g.

user_inputs = [bird, zoo, dog]

without using the .sort method, e.g.

user_inputs.sort

Can someone please point me in the right direction on how to perhaps
loop through all words in an array and compare them to each other using
< to find the "smallest" word in the array?

It sounds like you're needing to implement your own 'sort' method. I
suggest looking up some sorting algorithms and picking one to
implement (try http://en.wikipedia.org/wiki/Sorting_algorithm#Summaries_of_popular_sorting_algorithms).
Bubble sort is probably the easiest to implement, though it's not very
efficient.