https://docs.python.org/3/library/functions.html#sorted
sorted
(iterable[, key][, reverse])
Return a new sorted list from the items in iterable.
Has two optional arguments which must be specified as keyword arguments.
key specifies a function of one argument that is used to extract a comparison key from each list element:
key=str.lower
. The default value is None
(compare the elements directly).
reverse is a boolean value. If set to
True
, then the list elements are sorted as if each comparison were reversed.
Use
functools.cmp_to_key()
to convert an old-style cmp function to a key function.
The built-in
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 HOW TO.
基本範例:
沒有留言:
張貼留言