diff options
author | Juhana Jauhiainen <juhana.jauhiainen@gmail.com> | 2020-01-25 22:18:58 (GMT) |
---|---|---|
committer | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2020-01-25 22:18:58 (GMT) |
commit | 8271441d8b6e1f8eae1457c437da24e775801d9f (patch) | |
tree | afec03fa3f0abf4930596ce551ca4fe43054e31d /Doc/howto | |
parent | 4b09dc79f4d08d85f2cc945563e9c8ef1e531d7b (diff) | |
download | cpython-8271441d8b6e1f8eae1457c437da24e775801d9f.zip cpython-8271441d8b6e1f8eae1457c437da24e775801d9f.tar.gz cpython-8271441d8b6e1f8eae1457c437da24e775801d9f.tar.bz2 |
bpo-39374: Updated sorting documentation (GH-18177)
Diffstat (limited to 'Doc/howto')
-rw-r--r-- | Doc/howto/sorting.rst | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/Doc/howto/sorting.rst b/Doc/howto/sorting.rst index 1d6d5c4..a8efe65 100644 --- a/Doc/howto/sorting.rst +++ b/Doc/howto/sorting.rst @@ -43,16 +43,18 @@ Key Functions ============= Both :meth:`list.sort` and :func:`sorted` have a *key* parameter to specify a -function to be called on each list element prior to making comparisons. +function (or other callable) to be called on each list element prior to making +comparisons. For example, here's a case-insensitive string comparison: >>> sorted("This is a test string from Andrew".split(), key=str.lower) ['a', 'Andrew', 'from', 'is', 'string', 'test', 'This'] -The value of the *key* parameter should be a function that takes a single argument -and returns a key to use for sorting purposes. This technique is fast because -the key function is called exactly once for each input record. +The value of the *key* parameter should be a function (or other callable) that +takes a single argument and returns a key to use for sorting purposes. This +technique is fast because the key function is called exactly once for each +input record. A common pattern is to sort complex objects using some of the object's indices as keys. For example: |