# An Array of Arrays using Array.sort\_by

**URL:** <https://www.ruby-forum.com/t/an-array-of-arrays-using-array-sort-by/183180>\
**Category:** Ruby\
**Created:** [February 9, 2010, 4:41pm UTC](https://www.ruby-forum.com/t/an-array-of-arrays-using-array-sort-by/183180 "2010-02-09T16:41:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomzoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tomzoo](https://www.ruby-forum.com/u/tomzoo)\
**Post date:** [February 9, 2010, 4:41pm UTC](https://www.ruby-forum.com/t/an-array-of-arrays-using-array-sort-by/183180/1 "2010-02-09T16:41:39Z")

</div>

Hi,

I have:

myarray = Array.new  
mylist = [1,2]  
mylist2 = [3,1]  
mylist3 = [2,1]  
myarray.push(mylist)  
myarray.push(mylist2)  
myarray.push(mylist3)  
myarray.sort  
myarray.each do |x|  
puts x[0]  
end

And I would like to be able to sort myarray by the first or second  
elements in the mylists…

I have been on this problem for a few hours, ay help would be  
appreciated.

---

<div class="post-metadata">

**Author:** ![tomzoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tomzoo](https://www.ruby-forum.com/u/tomzoo)\
**Post date:** [February 9, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/an-array-of-arrays-using-array-sort-by/183180/2 "2010-02-09T16:45:21Z")

</div>

Tom E. wrote:

> Hi,
> 
> I have:
> 
> myarray = Array.new  
> mylist = [1,2]  
> mylist2 = [3,1]  
> mylist3 = [2,1]  
> myarray.push(mylist)  
> myarray.push(mylist2)  
> myarray.push(mylist3)  
> myarray.sort  
> myarray.each do |x|  
> puts x[0]  
> end
> 
> And I would like to be able to sort myarray by the first or second  
> elements in the mylists…
> 
> I have been on this problem for a few hours, ay help would be  
> appreciated.

Please ignore this. I have the answer by searching the forums… Which i  
should of done before i posted. I do apologies ☹ Can an Admin remove  
this post please?

---

<div class="post-metadata">

**Author:** ![tomzoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tomzoo](https://www.ruby-forum.com/u/tomzoo)\
**Post date:** [February 9, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/an-array-of-arrays-using-array-sort-by/183180/3 "2010-02-09T17:09:52Z")

</div>

2010/2/9 Tom E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Tom E. wrote:
> 
> > I have:
> > 
> > myarray = Array.new  
> > mylist = [1,2]  
> > mylist2 = [3,1]  
> > mylist3 = [2,1]  
> > myarray.push(mylist)  
> > myarray.push(mylist2)  
> > myarray.push(mylist3)

You can make your life a bit easier by doing

myarray = [  
[1,2],  
[3,1],  
[2,1],  
]

> Please ignore this. I have the answer by searching the forums… Which i  
> should of done before i posted. I do apologies ☹ Can an Admin remove  
> this post please?

No. 😉 Rather, please post your solution so others can benefit once  
they search through the archive.

Kind regards

robert
