Skip to content

Commit

Permalink
#22237: document that sorted() is guaranteed to be stable. Initial pa…
Browse files Browse the repository at this point in the history
…tch by Martin Panter.
  • Loading branch information
ezio-melotti committed Oct 28, 2014
1 parent 16e7f97 commit 9b1e92f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Doc/library/functions.rst
Expand Up @@ -1286,6 +1286,11 @@ are always available. They are listed here in alphabetical order.
Use :func:`functools.cmp_to_key` to convert an 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
<http://wiki.python.org/moin/HowTo/Sorting/>`_\.

Expand Down
4 changes: 3 additions & 1 deletion Doc/library/heapq.rst
Expand Up @@ -123,7 +123,6 @@ pushing all values onto a heap and then popping off the smallest values one at a
time::

>>> def heapsort(iterable):
... 'Equivalent to sorted(iterable)'
... h = []
... for value in iterable:
... heappush(h, value)
Expand All @@ -132,6 +131,9 @@ time::
>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
[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
(such as task priorities) alongside the main record being tracked::

Expand Down

0 comments on commit 9b1e92f

Please sign in to comment.