From ff64b3a532cadf8d109f5a7e44cc226e61df630f Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 26 Apr 2016 01:09:32 -0700 Subject: [PATCH] Issue #24715: Improve sort stability example --- Doc/howto/sorting.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Doc/howto/sorting.rst b/Doc/howto/sorting.rst index b501e0e1a40..675ed97a777 100644 --- a/Doc/howto/sorting.rst +++ b/Doc/howto/sorting.rst @@ -274,7 +274,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)] * To create a standard sort order for a class, just add the appropriate rich comparison methods: