python sort by list code example
Example 1: 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 2: pythonn sort example
gList = [ "Rocket League", "Valorant", "Grand Theft Autu 5"]
gList.sort()
Example 3: sort in python'
arr = [1,4,2,7,5,6]
arr = arr.sort()
arr = arr.sort(reverse = True)
Example 4: python sort
nums = [4,8,5,2,1]
sorted_nums = sorted(nums)
print(sorted_nums)
print(nums)
nums.sort()
print(nums)