Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
890a4b92
Commit
890a4b92
authored
Oct 21, 2018
by
Xtreak
Committed by
Raymond Hettinger
Oct 20, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-35020: Link to sorting examples from list.sort() (GH-9931)
parent
eeab510b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
2 deletions
+14
-2
Doc/howto/sorting.rst
Doc/howto/sorting.rst
+12
-1
Doc/library/stdtypes.rst
Doc/library/stdtypes.rst
+2
-1
No files found.
Doc/howto/sorting.rst
View file @
890a4b92
...
...
@@ -145,6 +145,17 @@ ascending *age*, do the *age* sort first and then sort again using *grade*:
>>> sorted(s, key=attrgetter('grade'), reverse=True) # now sort on primary key, descending
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
This can be abstracted out into a wrapper function that can take a list and
tuples of field and order to sort them on multiple passes.
>>> def multisort(xs, specs):
... for key, reverse in reversed(specs):
... xs.sort(key=attrgetter(key), reverse=reverse)
... return xs
>>> multisort(list(student_objects), (('grade', True), ('age', False)))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
The `Timsort <https://en.wikipedia.org/wiki/Timsort>`_ algorithm used in Python
does multiple sorts efficiently because it can take advantage of any ordering
already present in a dataset.
...
...
@@ -246,7 +257,7 @@ To convert to a key function, just wrap the old comparison function:
.. testsetup::
from functools import cmp_to_key
>>>
from functools import cmp_to_key
.. doctest::
...
...
Doc/library/stdtypes.rst
View file @
890a4b92
...
...
@@ -1201,6 +1201,8 @@ application).
--- this is helpful for sorting in multiple passes (for example, sort by
department, then by salary grade).
For sorting examples and a brief sorting tutorial, see :ref:`sortinghowto`.
.. impl-detail::
While a list is being sorted, the effect of attempting to mutate, or even
...
...
@@ -4752,4 +4754,3 @@ types, where they are relevant. Some of these are not reported by the
.. [5] To format only a tuple you should therefore provide a singleton tuple whose only
element is the tuple to be formatted.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment