Skip to content

Commit 53ca774

Browse files
authored
sorting howto: Add clarification on < using __lt__ (#92010)
1 parent 81120b6 commit 53ca774

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Doc/howto/sorting.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Odd and Ends
325325
>>> standard_way
326326
[('red', 1), ('red', 2), ('blue', 1), ('blue', 2)]
327327

328-
* The sort routines are guaranteed to use :meth:`__lt__` when making comparisons
328+
* The sort routines use ``<`` when making comparisons
329329
between two objects. So, it is easy to add a standard sort order to a class by
330330
defining an :meth:`__lt__` method:
331331

@@ -335,6 +335,9 @@ Odd and Ends
335335
>>> sorted(student_objects)
336336
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
337337

338+
However, note that ``<`` can fall back to using :meth:`__gt__` if
339+
:meth:`__lt__` is not implemented (see :func:`object.__lt__`).
340+
338341
* Key functions need not depend directly on the objects being sorted. A key
339342
function can also access external resources. For instance, if the student grades
340343
are stored in a dictionary, they can be used to sort a separate list of student

0 commit comments

Comments
 (0)