Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a0dcc5

Browse files
authored
bpo-45246: Document that sorted() only uses "<" comparisons (GH-28494)
1 parent a356272 commit 9a0dcc5

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Doc/library/functions.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,15 @@ are always available. They are listed here in alphabetical order.
15921592
compare equal --- this is helpful for sorting in multiple passes (for
15931593
example, sort by department, then by salary grade).
15941594

1595+
The sort algorithm uses only ``<`` comparisons between items. While
1596+
defining an :meth:`~object.__lt__` method will suffice for sorting,
1597+
:PEP:`8` recommends that all six :ref:`rich comparisons
1598+
<comparisons>` be implemented. This will help avoid bugs when using
1599+
the same data with other ordering tools such as :func:`max` that rely
1600+
on a different underlying method. Implementing all six comparisons
1601+
also helps avoid confusion for mixed type comparisons which can call
1602+
reflected the :meth:`~object.__gt__` method.
1603+
15951604
For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`.
15961605

15971606
.. decorator:: staticmethod

0 commit comments

Comments
 (0)