CKTIS / Python
sorted()
Builds a new sorted list from the members of an iterable.
Version: Python 3.14; repository image, production runtime not attested
Syntax
sorted(iterable, /, *, key=None, reverse=False)Builds a new sorted list from the members of an iterable.
Sorting is stable among equal keys and leaves the source list in its original order.
The return is a new list sorted using less-than comparisons. A key function supplies one comparison key per input item. Length one places a first and stability preserves the order bb then cc among length-two items. reverse=True reverses the ordering criterion while retaining stability at ties.
a = ["bb", "a", "cc"]
assert sorted(a, key=len) == ["a", "bb", "cc"] and a == ["bb", "a", "cc"]
assert sorted(a, key=len, reverse=True) == ["bb", "cc", "a"]
Examples
words = ["bb", "a", "cc"]
print(sorted(words, key=len))
print(words)
Result
['a', 'bb', 'cc'] ['bb', 'a', 'cc']
The return is a new list sorted using less-than comparisons. A key function supplies one comparison key per input item. Length one places a first and stability preserves the order bb then cc among length-two items. reverse=True reverses the ordering criterion while retaining stability at ties.