Commit aff394b6 authored by Raymond Hettinger's avatar Raymond Hettinger

A number of small fixups for the sorting howto guide.

parent 9522595d
...@@ -111,6 +111,15 @@ sort by *grade* then by *age*: ...@@ -111,6 +111,15 @@ sort by *grade* then by *age*:
>>> sorted(student_objects, key=attrgetter('grade', 'age')) >>> sorted(student_objects, key=attrgetter('grade', 'age'))
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)] [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
The :func:`operator.methodcaller` function makes method calls with fixed
parameters for each object being sorted. For example, the :meth:`str.count`
method could be used to compute message priority by counting the
number of exclamation marks in a message:
>>> messages = ['critical!!!', 'hurry!', 'standby', 'immediate!!']
>>> sorted(messages, key=methodcaller('count', '!'))
['standby', 'hurry!', 'immediate!!', 'critical!!!']
Ascending and Descending Ascending and Descending
======================== ========================
...@@ -259,8 +268,8 @@ Odd and Ends ...@@ -259,8 +268,8 @@ Odd and Ends
* For locale aware sorting, use :func:`locale.strxfrm` for a key function or * For locale aware sorting, use :func:`locale.strxfrm` for a key function or
:func:`locale.strcoll` for a comparison function. :func:`locale.strcoll` for a comparison function.
* The *reverse* parameter still maintains sort stability (i.e. records with * The *reverse* parameter still maintains sort stability (so that records with
equal keys retain the original order). Interestingly, that effect can be equal keys retain their original order). Interestingly, that effect can be
simulated without the parameter by using the builtin :func:`reversed` function simulated without the parameter by using the builtin :func:`reversed` function
twice: twice:
...@@ -275,12 +284,16 @@ Odd and Ends ...@@ -275,12 +284,16 @@ Odd and Ends
>>> sorted(student_objects) >>> sorted(student_objects)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
For general purpose comparisons, the recommended approach is to define all six
rich comparison operators. The :func:`functools.total_ordering` class
decorator makes this easy to implement.
* Key functions need not depend directly on the objects being sorted. A key * Key functions need not depend directly on the objects being sorted. A key
function can also access external resources. For instance, if the student grades function can also access external resources. For instance, if the student grades
are stored in a dictionary, they can be used to sort a separate list of student are stored in a dictionary, they can be used to sort a separate list of student
names: names:
>>> students = ['dave', 'john', 'jane'] >>> students = ['dave', 'john', 'jane']
>>> newgrades = {'john': 'F', 'jane':'A', 'dave': 'C'} >>> grades = {'john': 'F', 'jane':'A', 'dave': 'C'}
>>> sorted(students, key=newgrades.__getitem__) >>> sorted(students, key=grades.__getitem__)
['jane', 'dave', 'john'] ['jane', 'dave', 'john']
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