python sort list ascending code example
Example 1: python sort list in reverse
list.sort(reverse=True)
sorted(list, reverse=True)
Example 2: python sort a list by a custom order
list_to_sort = [('U', 23), ('R', 42), ('L', 17, 'D')]
custom_sort_order = ['R', 'D', 'L', 'U']
sorted(list_to_sort, key=lambda list_to_sort: custom_sort_order.index(list_to_sort[0]))
--> [('R', 42), ('L', 17, 'D'), ('U', 23)]
Example 3: python sort list
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
sortedVowels = sorted(vowels)
Example 4: how to sort list in ascending order in python
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
print('Sorted list:', vowels)