In a couple of threads there was a discussion on what was best to store messages in a database like MySQL. One of the threads said that the messages should be given a thread id and also have the parent child scheme implemented. The thread id is for fast retrieval and then the messages be sorted out by thread in the program so as not to put stress on the database. The other way is basically to recursively query the database for the children of a parent. Get the parent message and then query for its children and then query for their children ad infinitum and due to the recursion unraveling at the end the messages should be in the correct order.


I've tried to figure out how to sort out the messages without using a recursive way and by retrieveing the messages by thread id and then sorting them to get the correct order for the threads. However the answer eludes me. Is there a way to use sort to get the messages in the correct order for threading?
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 } ];
What I'm trying to get from
messageId parent
1          1
2          1
3          1
4          2
5          3
6          2
7          4

to
1          1
2          1
4          2
7          4
6          2
3          1
5          3

Can anyone offer a nudge in the right direction for this please?

My thanks for helping me keep my hair,

BMaximus

In reply to 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.