OneBite.Dev - Coding blog in a bite size

sort items in array by desc in python

Code snippet on how to sort items in array by desc in python

my_list = [8, 10, 6, 2, 4]
my_list.sort(reverse=True)

This code will sort the items in the array stored in the variable my_list, from highest to lowest. First, the variable my_list is declared with an array of integers. Then, using the sort method, the list is sorted on the ascending order by default. By passing the reverse argument, the order of sorting is reversed to descending. So, after running this code, my_list holds the numbers in descending order: [10, 8, 6, 4, 2].

python