Sort dependencies and nodes.