Sort by key function.
Make all test pass on Python 3.