PythonListsorted
copy

sortedList

list

Returns a new sorted list from an iterable.

Syntax

sorted(iterable, key?, reverse?)

Example

Enter values below to update the example in real time.

nums
result
sorted
print
words
key
nums = [3, 1, 4, 1, 5, 9]
result = sorted(nums)
print(result)  # [1, 1, 3, 4, 5, 9]
print(nums)    # [3, 1, 4, 1, 5, 9]  (원본 유지)

words = ["banana", "apple", "cherry"]
print(sorted(words, key=len))
# ['apple', 'banana', 'cherry']