Fix topological sort

Fixes https://github.com/django/django/pull/3525
I will restore it but the test case itself no longer depends on equality testing (as equality between operations is no longer implemented).