sorting arrays within an array by the first object in...

M

Mike Dershowitz

Hello:

Here's a bit of a tongue-twister:

I have a parent array of child arrays.

Each child array contains a set of objects. These objects are all from
the same model.

I know that each child array contains at least one object. I want to
sort on a field in the first object in each child array, and arrange the
child arrays in the parent array according to that field in the first
object in the child arrays. What's the best way to do this?

I thought it was some permutation of sort_by, but I tried a few options
and didn't have any luck. I wanted to take a step back and check in
with the community to see if I could get some direction.

Thanks very much in advance.

Mike
 
J

James Edward Gray II

I have a parent array of child arrays.

Each child array contains a set of objects. These objects are all
from
the same model.

I know that each child array contains at least one object. I want to
sort on a field in the first object in each child array, and
arrange the
child arrays in the parent array according to that field in the first
object in the child arrays. What's the best way to do this?

parent_array.sort_by { |child_array|
child_array.first.field_name_here }

If I understood the question correctly.

James Edward Gray II
 
J

John Joyce

parent_array.sort_by { |child_array|
child_array.first.field_name_here }

If I understood the question correctly.

James Edward Gray II
You have to wonder though... If you need the parent sorted, do you
also need the child arrays each sorted? Or do you want them as they
come in?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top