# Sorting through an array of an array

**URL:** <https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072>\
**Category:** Ruby\
**Created:** [October 14, 2006, 12:14am UTC](https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072 "2006-10-14T00:14:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 14, 2006, 12:14am UTC](https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072/1 "2006-10-14T00:14:37Z")

</div>

Hello.

Each of my arrays look like this:  
container[0] = [“foo”, “bar”, “baz”]  
container[1] = [“doo”, “poo”, “woo”]

i’d like to sort by the second position (bar, and poo)  
and of course,affecting the whole row, so that “bar” should be on top,  
along with “foo” and “baz” as container[0].

What method should i use?

Thanks

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 14, 2006, 12:26am UTC](https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072/2 "2006-10-14T00:26:17Z")

</div>

On 10/13/06, Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What method should i use?

container.sort { |row1, row2| row1[1] \<=\> row2[1] }

Josh

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 14, 2006, 12:36am UTC](https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072/3 "2006-10-14T00:36:20Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What method should i use?  
> container.sort { |x,y| x[1]\<=\>y[1] }

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 14, 2006, 12:44am UTC](https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072/4 "2006-10-14T00:44:22Z")

</div>

On 10/13/06, Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What method should i use?  
> Actually, the better way to sort in this scenario is to use sort\_by.

container.sort\_by {|x| x[1]}

No need to mess with the \<=\> operator by hand.

Brian.

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 14, 2006, 1:02am UTC](https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072/5 "2006-10-14T01:02:33Z")

</div>

But how to do apply this to having 3 elements in the value of an array?

container.sort\_by { |x,y,z| _lost here_  
puts x   
  
puts y   
  
puts z   
  
}

Brian M. wrote:

> On 10/13/06, Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > What method should i use?  
> > Actually, the better way to sort in this scenario is to use sort\_by.
> 
> container.sort\_by {|x| x[1]}
> 
> No need to mess with the \<=\> operator by hand.
> 
> Brian.

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 14, 2006, 1:14am UTC](https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072/6 "2006-10-14T01:14:24Z")

</div>

container.sort\_by {|x| x[2]}

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 14, 2006, 1:12am UTC](https://www.ruby-forum.com/t/sorting-through-an-array-of-an-array/72072/7 "2006-10-14T01:12:24Z")

</div>

On 10/13/06, Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But how to do apply this to having 3 elements in the value of an array?
> 
> container.sort\_by { |x,y,z| _lost here_  
> puts x   
>   
> puts y   
>   
> puts z   
>   
> }

Having more entries in the array doesn’t change the number of  
arguments to the block.

a = []  
a \<\< [1,2,3]  
a \<\< [4,5,6]  
a \<\< [7,8,9]

a.sort\_by do |element|  
[element[0], element[1], element[2]]  
end

If you return an Array from sort\_by’s block, it will sort on the  
entries of the array in order.

In this trivial case, since you’re sorting by all three, you could  
just write the above as:  
a.sort\_by {|e| e}  
(since ‘e’ is already an Array in the proper order)
