Functional Functions

functools.cmp_to_key()

Adapt a three-way comparator into a key factory whose wrapper objects support ordering. Prefer direct key functions for new sorting code.

Formal Syntax & Structure

python syntaxpython
functools.cmp_to_key(mycmp, /)

Parameters & Attributes

NameTypeDescription
mycmpcallableTwo-argument comparator returning a negative, zero or positive value.

Return Value

callable

The callable result described above.

Working Example

Example Snippetpython
import functoolsprint(sorted(["bbb","a","cc"],key=functools.cmp_to_key(lambda a,b:len(a)-len(b))))
Python Reference