# Pine's tutorial "Word Sorter" exercise

**URL:** <https://www.ruby-forum.com/t/pines-tutorial-word-sorter-exercise/86090>\
**Category:** Ruby\
**Created:** [February 19, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/pines-tutorial-word-sorter-exercise/86090 "2007-02-19T19:46:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Randy\_Shipp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@Randy\_Shipp](https://www.ruby-forum.com/u/Randy_Shipp)\
**Post date:** [February 19, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/pines-tutorial-word-sorter-exercise/86090/1 "2007-02-19T19:46:35Z")

</div>

On Apr 1 2006, 8:12 pm, Jan\_K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## puts input.sort

* * *

To reset slightly, this is an exercise that tells you to accept words  
from the user until they just hit Enter, then alphabetize the list.

My problem with the exercise is that the sort method alphabetizes  
capital letters before lowercase letters. Therefore, if input ==  
[ant, bear, Zebra], the output of

puts input.sort

is

Zebra  
ant  
bear

…which is clearly not what most users will expect. Short of  
changing the line in the program to read

input.push gets.chomp.downcase

(thus altering the entered words in a possibly undesirable way), how  
can we get the sort to work the way we might expect? Thanks in  
advance.

Randy…

---

<div class="post-metadata">

**Author:** ![Randy\_Shipp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@Randy\_Shipp](https://www.ruby-forum.com/u/Randy_Shipp)\
**Post date:** [February 19, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/pines-tutorial-word-sorter-exercise/86090/2 "2007-02-19T19:54:36Z")

</div>

#sort and #sort\_by accept blocks that tell how to do the comparison, so:

puts input.sort\_by{|a| a.downcase}

is the simplest solution.

PLUG: The RubyMentor project is looking for newbies to help!  
[http://rubymentor.rubyforge.org/wiki/wiki.pl](http://rubymentor.rubyforge.org/wiki/wiki.pl)
