# Ordering Steps in a Program

**URL:** <https://www.ruby-forum.com/t/ordering-steps-in-a-program/172299>\
**Category:** Ruby\
**Created:** [July 18, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/ordering-steps-in-a-program/172299 "2009-07-18T21:23:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/ordering-steps-in-a-program/172299/1 "2009-07-18T21:23:00Z")

</div>

Here is a simple program that stores entered words in an array, then,  
once a blank line is entered, displays them sorted alphabetically  
(another example from Pine’s book.) Here are two ways of accomplishing  
this.

#1:  
puts ‘Enter words to sort them alphabetically. When you are finished,  
press “return” on an empty line’

words = []

while true  
word = gets.chomp  
if word == ‘’  
break  
end  
words.push word  
end

puts ‘Here they are:’  
puts words.sort

–

#2:  
puts ‘Use this application to sort any number of words. Enter them  
below:’

words = []

while true  
word = gets.chomp  
words.push word  
if word ==’’  
break  
end  
end

puts ‘Here are the words you entered, sorted:’  
puts words.sort

–

Which one is more correct? The former was presented by Pine as a  
possible solution, and the latter is what I came up with after  
consulting this suggestion.

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 10:19pm UTC](https://www.ruby-forum.com/t/ordering-steps-in-a-program/172299/2 "2009-07-18T22:19:43Z")

</div>

On Sat, Jul 18, 2009 at 3:23 PM, Max N.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> words.push word  
> Which one is more correct? The former was presented by Pine as a  
> possible solution, and the latter is what I came up with after  
> consulting this suggestion.

The difference is that yours will include the empty string in the  
array to be sorted.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 10:57pm UTC](https://www.ruby-forum.com/t/ordering-steps-in-a-program/172299/3 "2009-07-18T22:57:46Z")

</div>

> The difference is that yours will include the empty string in the  
> array to be sorted.

Yes, I see–because the push method is before the if.
