Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b1e92f

Browse files
committed
#22237: document that sorted() is guaranteed to be stable. Initial patch by Martin Panter.
1 parent 16e7f97 commit 9b1e92f

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

Doc/library/functions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,11 @@ are always available. They are listed here in alphabetical order.
12861286
Use :func:`functools.cmp_to_key` to convert an old-style *cmp* function to a
12871287
*key* function.
12881288

1289+
The built-in :func:`sorted` function is guaranteed to be stable. A sort is
1290+
stable if it guarantees not to change the relative order of elements that
1291+
compare equal --- this is helpful for sorting in multiple passes (for
1292+
example, sort by department, then by salary grade).
1293+
12891294
For sorting examples and a brief sorting tutorial, see `Sorting HowTo
12901295
<http://wiki.python.org/moin/HowTo/Sorting/>`_\.
12911296

Doc/library/heapq.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ pushing all values onto a heap and then popping off the smallest values one at a
123123
time::
124124

125125
>>> def heapsort(iterable):
126-
... 'Equivalent to sorted(iterable)'
127126
... h = []
128127
... for value in iterable:
129128
... heappush(h, value)
@@ -132,6 +131,9 @@ time::
132131
>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
133132
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
134133

134+
This is similar to ``sorted(iterable)``, but unlike :func:`sorted`, this
135+
implementation is not stable.
136+
135137
Heap elements can be tuples. This is useful for assigning comparison values
136138
(such as task priorities) alongside the main record being tracked::
137139

0 commit comments

Comments
 (0)