# Sorting an NArray

**URL:** <https://www.ruby-forum.com/t/sorting-an-narray/80548>\
**Category:** Ruby\
**Created:** [December 28, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/sorting-an-narray/80548 "2006-12-28T16:48:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mekondelta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mekondelta](https://www.ruby-forum.com/u/mekondelta)\
**Post date:** [December 28, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/sorting-an-narray/80548/1 "2006-12-28T16:48:21Z")

</div>

I have a 2 dimensional array using NArray and wish to sort the values  
based  
on the values in column zero or row zero.

How can I achieve this using NArray? I can’t even seem to get the basic  
sort  
working:-

matrix.sort(0)

gives me a “…blah…:in `sort': undefined method `\<=\>’ for nil:NilClass  
(NoMethodError)”

I know I can supply a block to the sort method but the documentation,  
such  
that it is at:-

[http://narray.rubyforge.org/SPEC.en](http://narray.rubyforge.org/SPEC.en)

isn’t that informative and implies that the above line should work?

Any ideas greatly appreciated.

Cheers, Chris.

---

<div class="post-metadata">

**Author:** ![mekondelta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mekondelta](https://www.ruby-forum.com/u/mekondelta)\
**Post date:** [December 28, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/sorting-an-narray/80548/2 "2006-12-28T17:36:37Z")

</div>

On Fri, 29 Dec 2006, mekondelta wrote:

> (NoMethodError)"  
> Cheers, Chris.

harp:~ \> ruby -r narray -e’ p NArray.to\_na([[5,4,3], [2,1,0] ]) ’  
NArray.int(3,2):  
[[ 5, 4, 3],  
[2, 1, 0] ]

harp:~ \> ruby -r narray -e’ p NArray.to\_na([[5,4,3], [2,1,0]  
]).shape ’  
[3, 2]

harp:~ \> ruby -r narray -e’ p NArray.to\_na([[5,4,3], [2,1,0]  
]).sort ’  
NArray.int(3,2):  
[[ 0, 1, 2],  
[3, 4, 5] ]

# 

# sort the 0th thru 0th dims

# 

harp:~ \> ruby -r narray -e’ p NArray.to\_na([[5,4,3], [2,1,0]  
]).sort(0) ’  
NArray.int(3,2):  
[[ 3, 4, 5],  
[0, 1, 2] ]

# 

# sort the 0th thru 1st dims

# 

harp:~ \> ruby -r narray -e’ p NArray.to\_na([[5,4,3], [2,1,0]  
]).sort(1) ’  
NArray.int(3,2):  
[[ 0, 1, 2],  
[3, 4, 5] ]

-a
