From: Raymond Hettinger Date: Tue, 26 Apr 2016 08:11:10 +0000 (-0700) Subject: Issue #24715: Improve sort stability example X-Git-Tag: v3.6.0a1~114^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b9531bcdcce35c443f1b7c6f1c79ed8ae575f36b;p=thirdparty%2FPython%2Fcpython.git Issue #24715: Improve sort stability example --- diff --git a/Doc/howto/sorting.rst b/Doc/howto/sorting.rst index 10cb94cd0f4d..0334b2665754 100644 --- a/Doc/howto/sorting.rst +++ b/Doc/howto/sorting.rst @@ -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