No matter what kind of table you're setting up, you should always have a field for record ID (PRIMARY KEY AUTO INCREMENT or whatever). Since records from SELECT are returned in order of the PRIMARY KEY (the same order in which they were created), and you're pushing them into arrays, proper order should be retained for children of the same parent. If instead you want them in a different order, like say ordered by last modified time instead of created time, then just use an ORDER BY in your query. Children of the same parent should now be ordered by modified time, without having to change the basic algorithm.
Incidently, here's a modified version of the function for assigning everything to an array:
my @all;
construct($_, \@all) for @threads;
print "$_->{'id'} $_->{'title'}\n" for @all;
sub construct {
my ($p, $all) = @_;
push @$all, $p;
construct($_, $all) for @{$p->{'children'}};
}