This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve documentation of list.sort and sorted()
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: georg.brandl, olau, rhettinger
Priority: low Keywords:

Created on 2009-11-03 18:03 by olau, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (8)
msg94863 - (view) Author: Ole Laursen (olau) Date: 2009-11-03 18:03
On my Python 3.1, help() for sorted returns

sort(...)
    L.sort(key=None, reverse=False) -- stable sort *IN PLACE*

sorted(...)
    sorted(iterable, key=None, reverse=False) --> new sorted list

Kindly suggest this be expanded. Here's some text:

sort(...)

Sorts the sequence with a fast stable sort. The sequence is modified in
place. To remind you of this, the function always returns None. Example:

a = [1, 3, 2]
a.sort()
# a is now [1, 2, 3]

Use the "sorted()" built-in function if you need to preserve the
original list.

Set "reverse" to True to sort the elements in reverse order. A function
for extracting a key for comparison from each object can be passed in as
"key", e.g.

a = [{'k': 'foo'}, {'k': 'bar'}]
a.sort(key=lambda x: x['k'])
# a is now [{'k': 'bar'}, {'k': 'foo'}]

Note that "key" can be used to solve many sorting problems, e.g.
key=str.lower can be used for case-insensitive sorting and key=lambda x:
(x['a'], x['b']) can be used to sort by first 'a' then 'b'.

The sort is stable which means that the relative order of elements that
compare equal is not changed.


sorted(...)

Sorts the sequence with a fast stable sort and returns a new list with
the result. Example:

[same text as before]


I'm not sure how this interacts with what's in the online help
(http://docs.python.org/3.1/library/stdtypes.html search for "sort("),
maybe the text could just be copied over. I think it's important to give
copy-pasteable examples for something as important as this, and hint at
how you solve common sorting problems.
msg94864 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-11-03 18:14
Raymond, do you have an opinion on this?
msg94865 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-11-03 18:22
I'm inclined to leave the on-line help docstring as-is (pretty much
everywhere, we adopt a style of terse reminders instead of lengthy prose
with examples).  

Instead, was thinking of updating the sorting how-to and providing a
link to it from the main docs.
msg94868 - (view) Author: Ole Laursen (olau) Date: 2009-11-03 19:21
If you think it's too long, here's a shorter version:

Sorts sequence in place with a fast stable sort, returning None. key is
a function for extracting a comparison key from each element, e.g.
key=lambda x: x['name'] or key=str.lower. reverse=True reverses the
result order.

a = [1, 3, 2]
a.sort() # a is now [1, 2, 3]

Use the "sorted()" built-in function if you need to preserve the
original list.


Is this better? You could whack the last comment about sorted and/or the
example, then it's about the same length as the other built-in
docstrings (e.g. zip, reduce, getattr).
msg94869 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-11-03 19:49
Will look at it and make an update, but not right away.
msg94885 - (view) Author: Ole Laursen (olau) Date: 2009-11-04 10:11
OK, thanks! :) Sorry about the unintended nosy list removal, my browser 
got me there.
msg122020 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-21 23:29
After more thought, am leaving the doc strings as-is.  They are succinct and accurate.  I have updated the sorting how-to to more thoroughly cover the basics of sorting.
msg122112 - (view) Author: Ole Laursen (olau) Date: 2010-11-22 11:29
Okay. I can only say that while the current docstrings are likely good reminders for you, knowing Python in and out, they were pretty useless to me as documentation, which I believe docstrings should be, they're called docstrings, after all, not reminderstrings. :) I fail to see how including more info can hurt in any way, you're not forced to read it if you don't need it, so I hope you (or somebody else) will reconsider at some point.
History
Date User Action Args
2022-04-11 14:56:54adminsetgithub: 51506
2010-11-22 11:29:55olausetmessages: + msg122112
2010-11-21 23:29:28rhettingersetstatus: open -> closed
resolution: wont fix
messages: + msg122020
2009-11-04 10:11:10olausetmessages: + msg94885
2009-11-03 19:49:13rhettingersetmessages: + msg94869
2009-11-03 19:45:08georg.brandlsetnosy: + rhettinger
2009-11-03 19:21:59olausetnosy: - rhettinger
messages: + msg94868
2009-11-03 18:22:28rhettingersetpriority: low

messages: + msg94865
2009-11-03 18:14:14georg.brandlsetassignee: georg.brandl -> rhettinger

messages: + msg94864
nosy: + rhettinger
2009-11-03 18:03:20olaucreate