Python sorted() Function
Example
Sort a tuple:
a = ("b", "g", "a", "d", "f", "c", "h", "e")
x = slice(2)
print(a[x])
Run example »
Definition and Usage
The sorted()
function returns a sorted
list of the specified iterable object.
You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.
Note: You cannot sort a list that contains BOTH string values AND numeric values.
Syntax
sorted(iterable, key=key, reverse=reverse)
Parameter Values
Parameter | Description |
---|---|
iterable | Required. The sequence to sort, list, dictionary, tuple etc. |
key | Optional. A Function to execute to decide the order. Default is None |
reverse | Optional. A Boolean. False will sort ascending, True will sort descending. Default is False |
More Examples
Example
Sort ascending:
a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = sort(a,
reverse=True)
print(x)
Run example »