ID EN
Functools

cmp_to_key

Python 3.11

Transform an old-style comparison function to a key function. Used with tools that accept key functions (such as sorted(), min(), max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby()). This function is primarily used as a transition tool for programs being converted from Python 2 which supported the use of comparison functions.

Syntax

PYTHON
functools.cmp_to_key(func)

Examples

Example 1
PYTHON
sorted(iterable, key=cmp_to_key(locale.strcoll))   locale-aware sort order

See Also

key function sorted() min() max() heapq.nlargest() heapq.nsmallest() itertools.groupby() Sorting HOW TO