Sorting arrays within an array by the first object in

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

On Jun 13, 2007, at 8:39 AM, Mike D. wrote:

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 G. II

On Jun 13, 2007, at 8:45 AM, James Edward G. II wrote:

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 G. 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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs