Creating my own array sort method

I

Ilya B.

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?
 
J

Jesús Gabriel y Galán

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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top