Fixed #23660 -- Moved sort_dependencies to core.

https://code.djangoproject.com/ticket/23660
Can we be a little more specific? Maybe `RuntimeError` since this is akin to `'maximum recursion depth exceeded'`?