Topological sort

Allow for a topological sorting of a DAG to sort out dependencies.