Sort by nodes instead of connections, prevent cyclic connections from being made.