How to sort a list when certain values must appear later than others, potentially ignoring sort order for such items that need 'delaying'

This is called topological sorting. You can model "blocking" as edges of a directed graph. This should work if there are no circular "blockings".


I've done this in <100 lines of c# code (with comments). This implementation seems a little complicated.

Here is the outline of the algorithm

  1. Create a priority queue that is keyed by value that you want to sort by
  2. Insert all the items that do not have any "blocking" connections incoming
  3. While there are elements in the queue:
    1. Take an element of the queue. Put it in your resulting list.
    2. If there are any elements that were being directly blocked by this element and were not visited previously, put them into the queue (an element can have more than one blocking element, so you check for that)

A list of unprocessed elements should be empty at the end, or you had a cycle in your dependencies.

This is essentialy Topological sort with built in priority for nodes. Keep in mind that the result can be quite suprising depending on the number of connections in your graph (ex. it's possible to actually get elements that are in reverse order).


As Pratik Deoghare stated in their answer, you can use topological sorting. You can view your "dependencies" as arcs of a Directed Acyclic Graph (DAG). The restriction that the dependencies on the objects are acyclic is important as topological sorting is only possible "if and only if the graph has no directed cycles." The dependencies also of course don't make sense otherwise (i.e. a depends on b and b depends on a doesn't make sense because this is a cyclic dependency).

Once you do topological sorting, the graph can be interpreted as having "layers". To finish the solution, you need to sort within these layers. If there are no dependencies in the objects, this leads to there being just one layer where all the nodes in the DAG are on the same layer and then they are sorted based on their value.

The overall running time is still O(n log n) because topological sorting is O(n) and sorting within the layers is O(n log n). See topological sorting wiki for full running time analysis.