This does the job for your simple case with the only recursion necessary is that embedded inside Perl's builtin sort.

UpdateHowever, if your structure could have circular dependancies that are indirect--ie. A depends on B depends on C depends on A.--then this will not detect that.

Making it more efficient is the next challenge, but maybe that doesn't matter if your hash is smallish.

Updated to use @{} instead of $#{}. With thanks to Roy Johnson

#! perl -slw use strict; my %hash = ( MEX1J => { desc => 'Job 2', pred => [ 'TEX1J' ], }, MEX2J => { desc => 'Job end', pred => [ qw[TEX1J MEX1J] ], }, TEX1J => { desc => 'Job start', pred => [], } ); my @orderedKeys = sort{ return 0 unless @{ $hash{ $a }{ pred } } or @{ $hash{ $b }{ pred } }; my $AinB = grep /$a/, @{ $hash{ $b }{ pred } }; my $BinA = grep /$b/, @{ $hash{ $a }{ pred } }; die "Circular dependacy $a:$b" if $AinB and $BinA; return -1 if $AinB; return 1 if $BinA; return 0 unless $AinB or $BinA; } keys %hash; print "$_ $hash{ $_ }{ desc } @{ $hash{ $_ }{ pred } }" for @orderedKeys; __END__ P:\test>500240 TEX1J Job start MEX1J Job 2 TEX1J MEX2J Job end TEX1J MEX1J

A somewhat more efficient version curtesy of the Orcish Manouver(?) and List::Util::first that avoids modifying your datastructure.

#! perl -slw use strict; use List::Util qw[first]; my %hash = ( MEX1J => { desc => 'Job 2', pred => [ 'TEX1J' ], }, MEX2J => { desc => 'Job end', pred => [ qw[TEX1J MEX1J] ], }, TEX1J => { desc => 'Job start', pred => [], } ); my %cache; my @orderedKeys = sort{ return 0 unless @{ $hash{ $a }{ pred } } or @{ $hash{ $b }{ pred } }; my $AinB = $cache{ "$a|$b" } ||= first{ /$a/ } @{ $hash{ $b }{ pre +d } }; my $BinA = $cache{ "$b|$a" } ||= first{ /$b/ } @{ $hash{ $a }{ pre +d } }; die "Circular dependacy $a:$b" if $AinB and $BinA; return -1 if $AinB; return 1 if $BinA; return 0 unless $AinB or $BinA; } keys %hash; print "$_ $hash{ $_ }{ desc } @{ $hash{ $_ }{ pred } }" for @orderedKeys; __END__ P:\test>500240 TEX1J Job start MEX1J Job 2 TEX1J MEX2J Job end TEX1J MEX1J
</reduce>

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re: Sort Algorithm (recursive?) by BrowserUk
in thread Sort Algorithm (recursive?) by bertigo

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.