Skip to content

Commit aea92de

Browse files
bpo-45246: Document that sorted() only uses "<" comparisons (GH-28494)
(cherry picked from commit 9a0dcc5) Co-authored-by: Raymond Hettinger <[email protected]>
1 parent b06c3b3 commit aea92de

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Doc/library/functions.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,15 @@ are always available. They are listed here in alphabetical order.
15471547
compare equal --- this is helpful for sorting in multiple passes (for
15481548
example, sort by department, then by salary grade).
15491549

1550+
The sort algorithm uses only ``<`` comparisons between items. While
1551+
defining an :meth:`~object.__lt__` method will suffice for sorting,
1552+
:PEP:`8` recommends that all six :ref:`rich comparisons
1553+
<comparisons>` be implemented. This will help avoid bugs when using
1554+
the same data with other ordering tools such as :func:`max` that rely
1555+
on a different underlying method. Implementing all six comparisons
1556+
also helps avoid confusion for mixed type comparisons which can call
1557+
reflected the :meth:`~object.__gt__` method.
1558+
15501559
For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`.
15511560

15521561
.. decorator:: staticmethod

0 commit comments

Comments
 (0)