Forum: Ruby Pine's tutorial "Word Sorter" exercise

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
34b69d8f1106c55c78d24da01ec492e0?d=identicon&s=25 Randy Shipp (Guest)
on 2007-02-19 19:46
(Received via mailing list)
On Apr 1 2006, 8:12 pm, Jan_K <n...@none.com> 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...
7b4707f974af261f71943e1f2046c9ee?d=identicon&s=25 SonOfLilit (Guest)
on 2007-02-19 19:54
(Received via mailing list)
#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
This topic is locked and can not be replied to.