rewrite sortings in Python