~ubuntu-branches/ubuntu/precise/python3.2/precise

« back to all changes in this revision

Viewing changes to Doc/howto/sorting.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-02-14 16:12:14 UTC
  • mfrom: (10.1.3 experimental)
  • Revision ID: james.westby@ubuntu.com-20110214161214-f5vwa226kebccmt9
Tags: 3.2~rc3-1
Python 3.2 release candidate 3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    >>> sorted([5, 2, 3, 1, 4])
24
24
    [1, 2, 3, 4, 5]
25
25
 
26
 
You can also use the :meth:`list.sort` method of a list. It modifies the list
 
26
You can also use the :meth:`list.sort` method. It modifies the list
27
27
in-place (and returns *None* to avoid confusion). Usually it's less convenient
28
28
than :func:`sorted` - but if you don't need the original list, it's slightly
29
29
more efficient.
87
87
=========================
88
88
 
89
89
The key-function patterns shown above are very common, so Python provides
90
 
convenience functions to make accessor functions easier and faster. The operator
91
 
module has :func:`operator.itemgetter`, :func:`operator.attrgetter`, and
92
 
an :func:`operator.methodcaller` function.
 
90
convenience functions to make accessor functions easier and faster. The
 
91
:mod:`operator` module has :func:`~operator.itemgetter`,
 
92
:func:`~operator.attrgetter`, and an :func:`~operator.methodcaller` function.
93
93
 
94
94
Using those functions, the above examples become simpler and faster:
95
95
 
248
248
    [5, 4, 3, 2, 1]
249
249
 
250
250
In Python 3.2, the :func:`functools.cmp_to_key` function was added to the
251
 
functools module in the standard library.
 
251
:mod:`functools` module in the standard library.
252
252
 
253
253
Odd and Ends
254
254
============
256
256
* For locale aware sorting, use :func:`locale.strxfrm` for a key function or
257
257
  :func:`locale.strcoll` for a comparison function.
258
258
 
259
 
* The *reverse* parameter still maintains sort stability (i.e. records with
 
259
* The *reverse* parameter still maintains sort stability (so that records with
260
260
  equal keys retain the original order). Interestingly, that effect can be
261
261
  simulated without the parameter by using the builtin :func:`reversed` function
262
262
  twice: