Sort directly in python, hopefully fixes #1 and #2.