Issue 22237: sorted() docs should state that the sort is stable (original) (raw)

Created on 2014-08-20 16:53 by Wilfred.Hughes, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue22237.patch matrixise,2014-10-15 09:02 review
Messages (14)
msg225574 - (view) Author: Wilfred Hughes (Wilfred.Hughes) * Date: 2014-08-20 16:53
According to https://wiki.python.org/moin/HowTo/Sorting/#Sort_Stability_and_Complex_Sorts and Alex Martelli: http://stackoverflow.com/q/1915376/509706, Python's sorted() is stable. It would be great to update the docs for sorted() to state this.
msg225579 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-08-20 18:19
I agree: The docs for list.sort() do guarantee the stability, so sorted() should have the same indication.
msg225632 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-08-22 01:20
It looks like a fork of that how-to is actually part of the documentation: <https://docs.python.org/release/3.4.0/howto/sorting.html>. Perhaps the two should be linked better. If “sorted” is indeed meant to be stable, that makes the docstring for the “heapsort” function <https://docs.python.org/release/3.4.0/library/heapq.html#basic-examples> invalid. That function is not stable, for example: heapsort((0, 0, False, 0)) -> [0, False, 0, 0]. Also, the how-to implicitly guarantees that only less-than (__lt__) is required for comparison. This is already documented for “list.sort”, but it might be good to add that guarantee to the “sorted” reference. Maybe also clarify if it applies (or not) to other sorting and comparison routines (e.g. heapq, bisect, min, max), though maybe that is straying off scope for this bug.
msg225647 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-08-22 06:05
I'll update the docs for sorted().
msg229316 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2014-10-14 17:04
For this issue, I have read the source code of "sorted" and "list.sort" to be sure they use the same algorithm (not sure). But in the builtin_sorted function, I read PyId_sort, but when I grep the code, I don't find it. Where can I find the reference of this function? Thank you.
msg229326 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-10-14 18:16
PyId_sort is not a function, it's a somewhat complicated way of getting a Python string "sort" (in this case, for looking up a method using PyObject_GetAttrId). The string object is cached, with is faster than constructing one every time with PyObject_GetAttrString.
msg229415 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-10-15 08:38
> when I grep the code, I don't find it The non-greppability is due to preprocessor evil. The culprit is this macro from Include/object.h: #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname) along with the declaration _Py_IDENTIFIER(sort); earlier in bltinmodule.c.
msg229416 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2014-10-15 08:42
Hi Mark, Without your explanation, I was really lost. Thank you.
msg229419 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2014-10-15 09:02
My patch for the documentation of Python 3.5, just need a small feedback. If you agree with this patch, I will provide the same patch for 2.7 and 3.4 Thank you
msg229996 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2014-10-25 13:32
ping.
msg230051 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-10-27 00:29
The new text seems reasonable to me on its own, however I still think the heapsort() docstring needs updating as well
msg230140 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-10-28 12:00
New changeset d44f7d229e00 by Ezio Melotti in branch '2.7': #22237: document that sorted() is guaranteed to be stable. Initial patch by Martin Panter. https://hg.python.org/cpython/rev/d44f7d229e00 New changeset 5dd4906daa62 by Ezio Melotti in branch '3.4': #22237: document that sorted() is guaranteed to be stable. Initial patch by Martin Panter. https://hg.python.org/cpython/rev/5dd4906daa62 New changeset b01568e2597e by Ezio Melotti in branch 'default': #22237: merge with 3.4. https://hg.python.org/cpython/rev/b01568e2597e
msg230141 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-10-28 12:02
Fixed, thanks for the patch!
msg230145 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-10-28 13:00
New changeset af8f678a4a75 by Ezio Melotti in branch '2.7': #22237: fix patch attribution. https://hg.python.org/cpython/rev/af8f678a4a75 New changeset 2f697bcc8f86 by Ezio Melotti in branch '3.4': #22237: fix patch attribution. https://hg.python.org/cpython/rev/2f697bcc8f86 New changeset 7e870ddd1989 by Ezio Melotti in branch 'default': #22237: merge patch attribution fix. https://hg.python.org/cpython/rev/7e870ddd1989
History
Date User Action Args
2022-04-11 14:58:07 admin set github: 66433
2014-10-28 13:00:13 python-dev set messages: +
2014-10-28 12:02:49 ezio.melotti set status: open -> closedassignee: rhettinger -> ezio.melottinosy: + ezio.melottimessages: + resolution: fixedstage: resolved
2014-10-28 12:00:33 python-dev set nosy: + python-devmessages: +
2014-10-27 00:29:14 martin.panter set messages: +
2014-10-25 13:32:23 matrixise set messages: +
2014-10-15 09:02:53 matrixise set files: + issue22237.patchkeywords: + patchmessages: +
2014-10-15 08:42:00 matrixise set messages: +
2014-10-15 08:38:26 mark.dickinson set nosy: + mark.dickinsonmessages: +
2014-10-14 18:16:13 georg.brandl set messages: +
2014-10-14 17:04:05 matrixise set nosy: + matrixisemessages: +
2014-08-22 06:05:46 rhettinger set priority: normal -> lowmessages: + versions: - Python 3.1, Python 3.2, Python 3.3
2014-08-22 01:20:55 martin.panter set nosy: + martin.pantermessages: +
2014-08-20 19:55:04 rhettinger set assignee: docs@python -> rhettingernosy: + rhettinger
2014-08-20 18:19:03 georg.brandl set nosy: + georg.brandlmessages: +
2014-08-20 16:53:50 Wilfred.Hughes create