# Array.sort problem

**URL:** <https://www.ruby-forum.com/t/array-sort-problem/92624>\
**Category:** Ruby\
**Created:** [April 21, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/array-sort-problem/92624 "2007-04-21T20:21:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Larme](https://www.ruby-forum.com/u/Larme)\
**Post date:** [April 21, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/array-sort-problem/92624/1 "2007-04-21T20:21:43Z")

</div>

Dear all, I’m a newbie to ruby and today when I’m writing a simple  
script to process some data, I found something I can’t understand.

The data is stored in several column seperated by tab or space. I use  
the following code to get the data (assuming the data comes from  
standard input and all numbers are integer)

data=[]  
counter = 0  
while line = STDIN.gets  
data[counter] = line.split  
data[counter].map! {|str| str.to\_i}  
counter += 1  
end

hence data[i] is an array hold all the numbers in the ith line, data[i]  
[j] is the number on ith line and jth column. Then what I want the  
script to do is sorting lines according to a specified column. I  
thought the following code should work:

result = data.sort {|x, y| x[col] \<=\> y[col] }

where the col determine which column the script will sort according  
to. However ruby raise a error saying:  
"undefined method `<=>' for nil:NilClass (NoMethodError) from ana.rb:16:in`sort’  
from ana.rb:16  
"

I have to write the code as

result = data.sort {|x, y| x[col].to\_i \<=\> y[col].to\_i }

to let the script run properly. I’m quite confused here. I think the  
elements of array data are converted to integer when the code

data[counter].map! {|str| str.to\_i}

finished. However why ruby still requires a explicit conversion when I  
use the data.sort?

---

<div class="post-metadata">

**Author:** ![Larme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Larme](https://www.ruby-forum.com/u/Larme)\
**Post date:** [April 21, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/array-sort-problem/92624/2 "2007-04-21T20:49:47Z")

</div>

On 4/21/07, Larme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> data[counter] = line.split
> 
> to let the script run properly. I’m quite confused here. I think the  
> elements of array data are converted to integer when the code
> 
> data[counter].map! {|str| str.to\_i}
> 
> finished. However why ruby still requires a explicit conversion when I  
> use the data.sort?

Yes, they are. You might be doing some typo or other crap. Following  
program runs verbatim:

data=[]  
counter = 0  
while line = STDIN.gets  
data[counter] = line.split  
data[counter].map! {|str| str.to\_i}  
counter += 1  
end

#p data.sort\_by { |x| x[2] } #=\> you can use sort\_by as an alternative

p data.sort {|x,y| x[2] \<=\> y[2] } #=\> This also work.

---

<div class="post-metadata">

**Author:** ![Larme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Larme](https://www.ruby-forum.com/u/Larme)\
**Post date:** [April 21, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/array-sort-problem/92624/3 "2007-04-21T21:22:51Z")

</div>

Larme wrote:

> use the data.sort?  
> If you have an array arr with n elements which you all turn into  
> integers,  
> arr[i] with i\>=n will still be nil. So if in your case not all rows have  
> more  
> than col columns, that would explain the problem.

HTH,  
Sebastian H.

---

<div class="post-metadata">

**Author:** ![Larme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Larme](https://www.ruby-forum.com/u/Larme)\
**Post date:** [April 21, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/array-sort-problem/92624/4 "2007-04-21T21:30:51Z")

</div>

On 21.04.2007 20:16, Larme wrote:

> data[counter] = line.split
> 
> to let the script run properly. I’m quite confused here. I think the  
> elements of array data are converted to integer when the code
> 
> data[counter].map! {|str| str.to\_i}
> 
> finished. However why ruby still requires a explicit conversion when I  
> use the data.sort?

You probably have lines with differing number of entries and thus  
sometimes x[col] just returns nil.

You could do something like:

data.sort\_by {|x| x[col].to\_i}  
data.sort\_by {|x| x[col] || 0}

If you want to be sure that you extract only integers you could do

l.scan(/\d+/).map {|x| x.to\_i}

instead of the split.

Lots of options…

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Larme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Larme](https://www.ruby-forum.com/u/Larme)\
**Post date:** [April 22, 2007, 1:22am UTC](https://www.ruby-forum.com/t/array-sort-problem/92624/5 "2007-04-22T01:22:08Z")

</div>

On Apr 21, 2007, at 2:20 PM, Larme wrote:

> 1  
> 2 data=[]  
> 3 counter = 0  
> 4 while line = STDIN.gets  
> 5 data[counter] = line.split  
> 6 data[counter].map! {|str| str.to\_i}  
> 7 counter += 1  
> 8 end

Ok, so I’m a noob trying to learn something here…

Shouldn’t line 4 be presented with a double = (==) since you’re  
trying do something while line == STDIN.gets?

Also, does STDIN.gets need to be stated in a previous line? Or will  
it be prompted from within the ‘while’ statement.

--------------------------------------------| If you're not living on the edge, then you're just wasting space.

---

<div class="post-metadata">

**Author:** ![Larme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Larme](https://www.ruby-forum.com/u/Larme)\
**Post date:** [April 21, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/array-sort-problem/92624/6 "2007-04-21T21:41:52Z")

</div>

On Apr 22, 3:25 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > script to do is sorting lines according to a specified column. I
> 
> > use the data.sort?
> 
> l.scan(/\d+/).map {|x| x.to\_i}
> 
> instead of the split.
> 
> Lots of options…
> 
> Kind regards
> 
> ```
> robert
> 
> ```

Thank all of you. Yes, the problem is that the column number is not  
fixed – it should be, but my friend who prepare the data made some  
thing wrong.

---

<div class="post-metadata">

**Author:** ![Larme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Larme](https://www.ruby-forum.com/u/Larme)\
**Post date:** [April 22, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/array-sort-problem/92624/7 "2007-04-22T16:41:14Z")

</div>

On Apr 21, 2007, at 8:16 PM, Philip M. Gollucci wrote:

> > > 4 while line = STDIN.gets

> No, the assignment of line = STDIN.gets happens first which reads  
> up to a ‘\n’ including it. While then loops while this value is true.

Oh, so it’s not checking whether line == STDIN.gets (immediately),  
but rather assigning STDIN.gets to line, and then checking whether  
that’s true?

Nice, I learned something!

---------------------------------------------------------------|  
~Ari  
“I don’t suffer from insanity. I enjoy every minute of it” --1337est  
man alive

---

<div class="post-metadata">

**Author:** ![Larme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Larme](https://www.ruby-forum.com/u/Larme)\
**Post date:** [April 22, 2007, 2:17am UTC](https://www.ruby-forum.com/t/array-sort-problem/92624/8 "2007-04-22T02:17:11Z")

</div>

> > 4 while line = STDIN.gets

> Ok, so I’m a noob trying to learn something here…
> 
> Shouldn’t line 4 be presented with a double = (==) since you’re trying  
> do something while line == STDIN.gets?  
> No, the assignment of line = STDIN.gets happens first which reads up to  
> a ‘\n’ including it. While then loops while this value is true.

When there is no more inmput, line will be assigned nothing which is  
false and the loop ends.

This works identically to other languages like perl/python/php

## –

Philip M. Gollucci (removed\_email\_address@domain.invalid) 323.219.4708  
Consultant / [http://p6m7g8.net/Resume/resume.shtml](http://p6m7g8.net/Resume/resume.shtml)  
Senior Software Engineer - TicketMaster - [http://ticketmaster.com](http://ticketmaster.com)  
1024D/EC88A0BF 0DE5 C55C 6BF3 B235 2DAB B89E 1324 9B4F EC88 A0BF

Work like you don’t need the money,  
love like you’ll never get hurt,  
and dance like nobody’s watching.
