Skip to content

Commit 2e4d66d

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

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
@@ -1603,6 +1603,15 @@ are always available. They are listed here in alphabetical order.
16031603
compare equal --- this is helpful for sorting in multiple passes (for
16041604
example, sort by department, then by salary grade).
16051605

1606+
The sort algorithm uses only ``<`` comparisons between items. While
1607+
defining an :meth:`~object.__lt__` method will suffice for sorting,
1608+
:PEP:`8` recommends that all six :ref:`rich comparisons
1609+
<comparisons>` be implemented. This will help avoid bugs when using
1610+
the same data with other ordering tools such as :func:`max` that rely
1611+
on a different underlying method. Implementing all six comparisons
1612+
also helps avoid confusion for mixed type comparisons which can call
1613+
reflected the :meth:`~object.__gt__` method.
1614+
16061615
For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`.
16071616

16081617
.. decorator:: staticmethod

0 commit comments

Comments
 (0)