sort in descending order python code example
Example 1: python sort list in reverse order
sorted(list_name, reverse=True)
list_name.sort(reverse=True)
Example 2: python sort list in reverse
list.sort(reverse=True)
sorted(list, reverse=True)
Example 3: python sorted descending
sorted_list = sorted(list)
sorted_list = sorted(list, reverse=True)
Example 4: 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 5: python sort list
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
sortedVowels = sorted(vowels)
Example 6: sort an array python
myList = [1,5,3,4]
myList.sort()
print(myList)