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

Created on 2009-11-03 18:03 by olau, last changed 2009-11-04 10:11 by olau.

Messages (6)
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) Date: 2009-11-03 18:14
Raymond, do you have an opinion on this?
msg94865 - (view) Author: Raymond Hettinger (rhettinger) 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) 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.
History
Date User Action Args
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