python sorts lists using the sorted function
- 2020-05-05 11:27:01
- OfStack
This example shows how python sorts lists using the sorted function. Share with you for your reference. The details are as follows:
python provides the sorted function for sorting lists, and you can arrange
in either positive or reverse order
# Create a list of Numbers
numbers = [5, 1, 4, 3, 2, 6, 7, 9]
# Output sorted array of Numbers
print sorted(numbers)
# Output the original array, not changed
print numbers
my_string = ['aa', 'BB', 'zz', 'CC', 'dd', "EE"]
# Sort the list of strings in character order, which defaults to character order
print sorted(my_string)
# use reverse=True Sort by the reverse order of the first character
print sorted(strs, reverse=True)
## ['zz', 'aa', 'CC', 'BB']
I hope this article has been helpful to your Python programming.