build a key function directly instead of relying on cmp_to_key.

This approach is more direct, faster, and extends backwards compatibility from Python 2.7 to 2.6.