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

Discussion in 'Ruby' started by Mike Dershowitz, Jun 13, 2007.

  1. 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

    --
    Posted via http://www.ruby-forum.com/.
    Mike Dershowitz, Jun 13, 2007
    #1
    1. Advertising

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

    > 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
    James Edward Gray II, Jun 13, 2007
    #2
    1. Advertising

  3. Mike Dershowitz

    John Joyce Guest

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

    > On Jun 13, 2007, at 8:39 AM, Mike Dershowitz wrote:
    >
    >> 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
    >

    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?
    John Joyce, Jun 14, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,126
    Philipp
    Jan 20, 2009
  2. Abe
    Replies:
    2
    Views:
    167
  3. Nick Black

    matching arrays within arrays

    Nick Black, Jan 23, 2007, in forum: Ruby
    Replies:
    3
    Views:
    138
    Nick Black
    Jan 23, 2007
  4. BKennedy
    Replies:
    3
    Views:
    104
    BKennedy
    Dec 3, 2003
  5. MG
    Replies:
    5
    Views:
    184
    Thomas 'PointedEars' Lahn
    Apr 16, 2010
Loading...

Share This Page