Skip to content

Commit

Permalink
Issue python#24715: Improve sort stability example
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed Apr 26, 2016
1 parent b3b366d commit b9531bc
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Doc/howto/sorting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ Odd and Ends
twice:

>>> data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
>>> assert sorted(data, reverse=True) == list(reversed(sorted(reversed(data))))
>>> standard_way = sorted(data, key=itemgetter(0), reverse=True)
>>> double_reversed = list(reversed(sorted(reversed(data), key=itemgetter(0))))
>>> assert standard_way == double_reversed
>>> standard_way
[('red', 1), ('red', 2), ('blue', 1), ('blue', 2)]

* The sort routines are guaranteed to use :meth:`__lt__` when making comparisons
between two objects. So, it is easy to add a standard sort order to a class by
Expand Down

0 comments on commit b9531bc

Please sign in to comment.