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: Add list.sorted()
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: aleax, rhettinger
Priority: normal Keywords: patch

Created on 2003-10-18 00:34 by rhettinger, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
cs.diff rhettinger, 2003-10-18 00:34 Patch 1 with docs, unittests, and annoucement
cs2.diff rhettinger, 2003-10-18 12:42 Patch 2 avoids unnecessary copy
cs3.diff rhettinger, 2003-10-18 17:43 Patch 3 with better argument handling
sorted.diff rhettinger, 2003-10-29 00:40 Patch 4 implements class method list.sorted()
Messages (6)
msg44800 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-10-18 00:34
As discussed on py-dev, this returns a sorted copy of a
list while leaving the original intact.
msg44801 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-10-18 06:26
Logged In: YES 
user_id=80475

Revised the patch to avoid an unnecessary copy whenever the
underlying object is not referenced anywhere else -- this
occurs in the common use case:  for elem in
d.keys().copysort(): . . .
msg44802 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-10-18 17:41
Logged In: YES 
user_id=80475

Rationale for the change
---------------------
An inline sort is usable anywhere an expression is allowed.
 This includes important places like function call arguments
and list comprehensions:

  todo = [t for t in tasks.copysort() if due_today(t)]

  genhistory(date, events.copysort(key=incidenttime))

Spreading these out over multiple lines is an unnecessary
distractor from the problem domain, making the code harder
to understand and maintain.

Also, using copysort() eliminates a unnecessary variable
that changes state from unsorted to sorted and has a
lifetime longer than the data is actually needed. In longer
code fragments, this decreases code complexity, code length,
the number of variables, and decreases the risk of using a
variable in the wrong state which is a common source of
programming  errors.
msg44803 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-10-29 00:40
Logged In: YES 
user_id=80475

Alex, can you give this patch a second review.

I did not put in additional classmethod warnings because all
plausible misunderstandings raise an error anyway.
msg44804 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-10-29 06:56
Logged In: YES 
user_id=80475

Applied.
msg44805 - (view) Author: Alex Martelli (aleax) * (Python committer) Date: 2003-10-29 09:46
Logged In: YES 
user_id=60314

Looks good to me!  I understand not wanting -- given the clean and
spare code for list.sorted -- to go to the considerable trouble needed
to give more specific error messages; however, if we release like this
we WILL get questions about it on c.l.py and help@python.org -- "why doesn't ''for x in mydict.keys().sorted():'' work?!" etc.  Do you mind if
eventually I _do_ try to ensure the error messages specifically mention list.sorted being a classmethod and requiring one iterable argument?
History
Date User Action Args
2022-04-11 14:56:00adminsetgithub: 39428
2003-10-18 00:34:53rhettingercreate