Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Doc/howto/sorting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@ Sorting Basics
==============

A simple ascending sort is very easy: just call the :func:`sorted` function. It
returns a new sorted list:
returns a new sorted list and does not modify the original.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it's useless. HOWTO already very clear here and sorted docs also:

Return a new sorted list from the items in iterable.


If you do not store or otherwise use the return value, the sorted result is
lost:
Comment on lines +22 to +23
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also redundant, given above sentence.

No need to explain every basic language feature in the HOWTO. We have the Tutorial for this.


.. doctest::

>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

>>> original = [5, 2, 3, 1, 4]
>>> sorted(original) # Returns a new sorted list
[1, 2, 3, 4, 5]
>>> original # Original remains unchanged
[5, 2, 3, 1, 4]

>>> # To keep the sorted result, assign it to a variable
>>> sorted_list = sorted(original)
>>> sorted_list
[1, 2, 3, 4, 5]

You can also use the :meth:`list.sort` method. It modifies the list
in-place (and returns ``None`` to avoid confusion). Usually it's less convenient
than :func:`sorted` - but if you don't need the original list, it's slightly
Expand All @@ -32,7 +46,7 @@ more efficient.
.. doctest::

>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> a.sort() # Modifies 'a' in-place, returns None
Comment thread
NureddinSoltan marked this conversation as resolved.
Outdated
>>> a
[1, 2, 3, 4, 5]

Expand Down
Loading