in reply to "Intelligent" array joining

topological sort, aka tsort