[issue24715] Sorting HOW TO: bad example for reverse sort stability
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
New submission from Jakub Wilk: https://docs.python.org/3/howto/sorting.html#odd-and-ends gives the following example for reverse sort stability:
data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)] assert sorted(data, reverse=True) == list(reversed(sorted(reversed(data))))
But here all the keys are different, so the result would be the same even if the sort algorithm weren't stable. You probably wanted to pass to key=itemgetter(0) to both sorted() calls. ---------- assignee: docs@python components: Documentation messages: 247328 nosy: docs@python, jwilk priority: normal severity: normal status: open title: Sorting HOW TO: bad example for reverse sort stability _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue24715> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: docs@python -> rhettinger nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue24715> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- keywords: +patch Added file: http://bugs.python.org/file40022/reverse_stablity.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue24715> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Ezio Melotti added the comment: LGTM, however I would also show the sorted output too. ---------- nosy: +ezio.melotti stage: -> commit review type: -> enhancement versions: +Python 2.7, Python 3.5, Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue24715> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Roundup Robot added the comment: New changeset ba87f7f246e0 by Raymond Hettinger in branch '2.7': Issue #24715: Improve sort stability example https://hg.python.org/cpython/rev/ba87f7f246e0 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue24715> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Roundup Robot added the comment: New changeset ae1e55102449 by Raymond Hettinger in branch '3.5': Issue #24715: Improve sort stability example https://hg.python.org/cpython/rev/ae1e55102449 ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue24715> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- resolution: -> fixed status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue24715> _______________________________________
participants (4)
-
Ezio Melotti
-
Jakub Wilk
-
Raymond Hettinger
-
Roundup Robot