Một hàm của Python: sorted(
)
Đúng như tên gọi của nó thì hàm này có thể sắp xếp các phần tử ở trong một list, set hay tuple.
sorted
(iterable, *, key=None, reverse=False)
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 element in iterable (for example, 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).