Adding a generic topological sort for the update pipeline.

I'll review its uses.