diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-09-21 17:53:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-21 17:53:26 (GMT) |
commit | aea92de9900ceb8d679a9324fc72621940b9c8a0 (patch) | |
tree | ac54dd0b7ed8f5dad675d52f8c9aaa3cf227ec92 | |
parent | b06c3b364720ce8e8dfb74dfa24434e067ac89ba (diff) | |
download | cpython-aea92de9900ceb8d679a9324fc72621940b9c8a0.zip cpython-aea92de9900ceb8d679a9324fc72621940b9c8a0.tar.gz cpython-aea92de9900ceb8d679a9324fc72621940b9c8a0.tar.bz2 |
bpo-45246: Document that sorted() only uses "<" comparisons (GH-28494)
(cherry picked from commit 9a0dcc5b2e04d9c51350107734f12a1cbc0284a7)
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
-rw-r--r-- | Doc/library/functions.rst | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 39ce338..15bfbf4 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1547,6 +1547,15 @@ are always available. They are listed here in alphabetical order. compare equal --- this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). + The sort algorithm uses only ``<`` comparisons between items. While + defining an :meth:`~object.__lt__` method will suffice for sorting, + :PEP:`8` recommends that all six :ref:`rich comparisons + <comparisons>` be implemented. This will help avoid bugs when using + the same data with other ordering tools such as :func:`max` that rely + on a different underlying method. Implementing all six comparisons + also helps avoid confusion for mixed type comparisons which can call + reflected the :meth:`~object.__gt__` method. + For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`. .. decorator:: staticmethod |