Forum: Ruby An Array of Arrays using Array.sort_by

Posted by Tom Eaton (tomeaton)
on 2010-02-09 16:41
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.
Posted by Tom Eaton (tomeaton)
on 2010-02-09 16:45
Tom Eaton 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?
Posted by Robert Klemme (Guest)
on 2010-02-09 17:09
(Received via mailing list)
2010/2/9 Tom Eaton <te4t0n@gmail.com>:
> Tom Eaton 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.