Deterministic topological sort, needed for testing (reproduciblity)