It looks like the container of daughters is an anonymous array, so given your 'original data' and 'desired re-ordered data' it all amounts to re-ordering the daughters array by their name. Maybe you want a method 'walk_down_sorted' or monkey-patch the module in walk_down amounting to:

--- /usr/share/perl5/Tree/DAG_Node.pm 2018-02-13 23:16:43.000000000 + +0100 +++ /home/shmem/perl/lib/Tree/DAG_Node.pm 2018-04-23 03:46:29.60751 +5296 +0200 @@ -1661,7 +1661,7 @@ if(@daughters) { $o->{'_depth'} += 1; #print "Depth " , $o->{'_depth'}, "\n"; - foreach my $one (@daughters) { + foreach my $one (sort {$a->name cmp $b->name} @daughters) { $one->walk_down($o) if UNIVERSAL::can($one, 'is_node'); # and if it can do "is_node", it should provide a walk_down! }

result with that:

root HASH (0 ): { nc => 0, top_foo => 1 } a HASH (0:0 ): { nc => 1 } b HASH (0:0:0 ): { nc => 2 } c HASH (0:0:2 ): { nc => 6 } d HASH (0:0:2:0 ): { nc => 7 } e HASH (0:0:1 ): { nc => 3 } f HASH (0:0:1:0 ): { nc => 4 } g HASH (0:0:1:0:0 ): { nc => 5 } h HASH (0:1 ): { nc => 8 } i HASH (0:1:0 ): { nc => 9 } j HASH (0:1:0:0 ): { nc => 10 }

A sort callback for sorting daughters in walk_down would be nice to have. File an enhancement request ;-)

edit: this sorting callback should also be available while populating the 'daughters' anonymous array.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Re-ordering data branches in a [Tree::DAG_Node] tree by shmem
in thread Re-ordering data branches in a [Tree::DAG_Node] tree by atcroft

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.