Use key instead of cmp for sorting in Python 3

Python 3 removed the `cmp` parameter for list sorting [in favour of using a key function](http://python3porting.com/preparing.html#when-sorting-use-key-instead-of-cmp).

This PR changes that for Python 3 compatibility, and should work all the way back to Python 2.4.