This code does what you need, other than the fact that the children of a message aren't guaranteed to be in any particular order:
my $testarray = [ { messageId => 1, parent => 1 }, { messageId => 2, parent => 1 }, { messageId => 3, parent => 1 }, { messageId => 4, parent => 2 }, { messageId => 5, parent => 3 }, { messageId => 6, parent => 2 }, { messageId => 7, parent => 4 } ]; #wander down array, setting "children" field foreach my $key (@$testarray) { next if ($key->{parent} eq $key->{messageId}); # skip root nodes push @{$$testarray[$key->{parent} - 1]->{children}}, $key->{message +Id}; # messageIds are 1-relative, arrays are 0-relative } # build up a sorted array of elements... my @newstack; # a. retrieve each element in turn foreach my $message (@$testarray) { # b. if parent = messageid, push onto new stack, and go back to the be +ginning if ($message->{parent} eq $message->{messageId}) { push @newstack,$message; next; } # c. insert it into the new stack, just after the parent for (my $loop = scalar(@newstack) - 1;$loop >= 0;$loop--) { #d. if the last element IS the parent, just push it and move on if ($newstack[$loop]->{messageId} eq $message->{parent}) { push @newstack,$message; $loop = -1; next; } #e. move the stack down a place $newstack[$loop + 1] = $newstack[$loop]; #f. insert new value, if appropriate and trip out of loop if ($newstack[$loop - 1]->{messageId} == $message->{parent}) { $newstack[$loop] = $message; $loop = -1; } } } #print the results print "Message\tParent\n"; print $_->{messageId} . "\t" . $_->{parent} . "\n" for @newstack;

produces the results:

Message Parent 1 1 3 1 5 3 2 1 6 2 4 2 7 4

rdfield

update of course you could always retrieve the messages from the database in the correct order in the first place:

select messageId,parent from messages start with messageId = 1 connect by prior messageId = parent

In reply to Re: Sorting an Array of Hashes (AoH) by its keys as a threaded message by rdfield
in thread Sorting an Array of Hashes (AoH) by its keys as a threaded message by BMaximus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.