# Sorting an array based on user input

**URL:** <https://www.ruby-forum.com/t/sorting-an-array-based-on-user-input/237693>\
**Category:** Ruby\
**Created:** [November 24, 2013, 8:03pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-user-input/237693 "2013-11-24T20:03:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [November 24, 2013, 8:03pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-user-input/237693/1 "2013-11-24T20:03:15Z")

</div>

Hey, I have just finished the chapter “Ordering your Library” so I want  
to experiment a little more with arrays and sorting. I wrote some code  
that gets the user’s input on how many cities he’d like to have  
alphabetized, but I’m encountering some problems. Here’s the code:

def alphabetize(arr, rev = false)  
if rev == true  
arr.sort!  
arr.reverse!  
else  
arr.sort! {|item1, item2| item1 \<=\> item2}  
end  
arr.each {|thing| puts “#{thing}” + " "}  
end  
puts “How many cities would you like to specify?”  
n = gets.chomp.to\_i  
for i in 0…n  
puts (i+1).to\_s + ". city: "  
cities = Array.new(n)  
cities[i] = gets.chomp  
end  
alphabetize(cities)

So basically the user says how many cities he wants to sort and then  
specifies which ones. Reading the user’s input works just fine, but  
apparently, there’s some problem when the method is being called, since  
I keep getting this error:

comparison of NilClass with String failed

It has to be something in the method, right? I’m not that experienced  
with Ruby yet, so I’m kind of stuck here and I can’t think of a  
solution. Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![bearsofwar1337\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@bearsofwar1337\_S](https://www.ruby-forum.com/u/bearsofwar1337_S)\
**Post date:** [November 24, 2013, 8:28pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-user-input/237693/2 "2013-11-24T20:28:27Z")

</div>

The simplest thing to start with would be to feed a list into the  
function  
and see if that works:

cities = %w{london paris bejing tokyo moscow}  
alphabetize(cities)

This gives me

bejing  
london  
moscow  
paris  
tokyo

So the sort function seems ok. The real problem is this

puts “How many cities would you like to specify?”  
n = gets.chomp.to\_i  
for i in 0…n  
puts (i+1).to\_s + ". city: "  
cities = Array.new(n)  
cities[i] = gets.chomp  
end

Basically you delete the array every time you go round the loop.

puts “How many cities would you like to specify?”  
n = gets.chomp.to\_i  
cities = Array.new  
n.times.each do  
puts "City: "  
cities \<\< gets.chomp  
end

The creation of the array is outside the loop and do things in a more  
Ruby  
way

---

<div class="post-metadata">

**Author:** ![bearsofwar1337\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@bearsofwar1337\_S](https://www.ruby-forum.com/u/bearsofwar1337_S)\
**Post date:** [November 25, 2013, 11:08am UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-user-input/237693/3 "2013-11-25T11:08:01Z")

</div>

On Sun, Nov 24, 2013 at 8:27 PM, Peter H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The creation of the array is outside the loop and do things in a more Ruby  
> way

You could even do

puts “How many cities would you like to specify?”  
n = Integer(gets)

cities = Array.new n do  
puts "City: "  
gets.chomp  
end

🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![bearsofwar1337\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@bearsofwar1337\_S](https://www.ruby-forum.com/u/bearsofwar1337_S)\
**Post date:** [November 25, 2013, 5:40pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-user-input/237693/4 "2013-11-25T17:40:43Z")

</div>

Robert, clever!!! 🙂

On Mon, Nov 25, 2013 at 7:07 AM, Robert K.

---

<div class="post-metadata">

**Author:** ![bearsofwar1337\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@bearsofwar1337\_S](https://www.ruby-forum.com/u/bearsofwar1337_S)\
**Post date:** [November 25, 2013, 5:56pm UTC](https://www.ruby-forum.com/t/sorting-an-array-based-on-user-input/237693/5 "2013-11-25T17:56:43Z")

</div>

Peter H. wrote in post #1128490:

> n.times.each do

You probably already know this, but “.each” is redundant.
