Perform the sorting at the SQL level instead of Python.