Topological Sorting by nn

{a[n=NR]=$0}END{for(;n--;i=a[i]=_){for(;!/: *$/;)$0=a[++i]
sub(/: */,_)
print
for(j=NR;j;)sub(FS$0"( |$)",FS,a[j--])}}

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return top