Commit 2722e6a4 authored by Ezio Melotti's avatar Ezio Melotti

#22237: document that sorted() is guaranteed to be stable. Initial patch by Martin Panter.

parent 2af903a9
...@@ -1318,6 +1318,11 @@ available. They are listed here in alphabetical order. ...@@ -1318,6 +1318,11 @@ available. They are listed here in alphabetical order.
each element only once. Use :func:`functools.cmp_to_key` to convert an each element only once. Use :func:`functools.cmp_to_key` to convert an
old-style *cmp* function to a *key* function. old-style *cmp* function to a *key* function.
The built-in :func:`sorted` function is guaranteed to be stable. A sort is
stable if it guarantees not to change the relative order of elements that
compare equal --- 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 `Sorting HowTo For sorting examples and a brief sorting tutorial, see `Sorting HowTo
<http://wiki.python.org/moin/HowTo/Sorting/>`_\. <http://wiki.python.org/moin/HowTo/Sorting/>`_\.
......
...@@ -136,7 +136,6 @@ pushing all values onto a heap and then popping off the smallest values one at a ...@@ -136,7 +136,6 @@ pushing all values onto a heap and then popping off the smallest values one at a
time:: time::
>>> def heapsort(iterable): >>> def heapsort(iterable):
... 'Equivalent to sorted(iterable)'
... h = [] ... h = []
... for value in iterable: ... for value in iterable:
... heappush(h, value) ... heappush(h, value)
...@@ -145,6 +144,9 @@ time:: ...@@ -145,6 +144,9 @@ time::
>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0]) >>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
This is similar to ``sorted(iterable)``, but unlike :func:`sorted`, this
implementation is not stable.
Heap elements can be tuples. This is useful for assigning comparison values Heap elements can be tuples. This is useful for assigning comparison values
(such as task priorities) alongside the main record being tracked:: (such as task priorities) alongside the main record being tracked::
......
...@@ -1008,6 +1008,7 @@ Todd R. Palmer ...@@ -1008,6 +1008,7 @@ Todd R. Palmer
Juan David Ibáñez Palomar Juan David Ibáñez Palomar
Jan Palus Jan Palus
Yongzhi Pan Yongzhi Pan
Martin Panter
Mathias Panzenböck Mathias Panzenböck
M. Papillon M. Papillon
Peter Parente Peter Parente
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment