Try this, it'll sort the list in-place in descending order (there's no need to specify a key in this case):
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9] listB.sort(reverse=True) # listB gets modified print listB => [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]Alternatively, you can create a new sorted list:
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9] listC = sorted(listB, reverse=True) # listB remains untouched print listC => [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]Python list sort() function can be used to sort a List in ascending, descending, or user-defined order. In each case, the time complexity is O(nlogn) in Python.
Syntax of sort() function
Syntax:
List_name.sort(reverse=True/False, key=myFunc)Parameter:
- reverse: (Optional), reverse=True will sort the list descending. Default is reverse=False
- key Optional. A function to specify the sorting criteria(s)
Example 1: Sort the List of numbers in Ascending Order
The sort() method by default sort element in ascending order as we can see below example:
Python3
# Python program to demonestrate to
# sorting numbers in Ascending Order
numbers= [[1, 2, 3, 4]0[1, 2, 3, 4]1[1, 2, 3, 4]2[1, 2, 3, 4]1[1, 2, 3, 4]4[1, 2, 3, 4]1[1, 2, 3, 4]6[1, 2, 3, 4]7
[1, 2, 3, 4]8
[1, 2, 3, 4]9['code', 'geeks', 'ide', 'practice']0['code', 'geeks', 'ide', 'practice']1
[1, 2, 3, 4]9['code', 'geeks', 'ide', 'practice']3['code', 'geeks', 'ide', 'practice']4
[1, 2, 3, 4]9['code', 'geeks', 'ide', 'practice']6['code', 'geeks', 'ide', 'practice']7['code', 'geeks', 'ide', 'practice']8['code', 'geeks', 'ide', 'practice']4
[1, 2, 3, 4]9[4, 3, 2, 1]1[4, 3, 2, 1]2
[4, 3, 2, 1]3
Output:
[1, 2, 3, 4]Example 2: Sort the List of alphabets in Ascending Order
The sort() method sorts the list in order from A-Z, to a-z in the alphabet.
Python3
[4, 3, 2, 1]4
[4, 3, 2, 1]5
[4, 3, 2, 1]6= [[4, 3, 2, 1]9[1, 2, 3, 4]1[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]1[1, 2, 3, 4]1[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]3[1, 2, 3, 4]1[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]5[1, 2, 3, 4]7
[1, 2, 3, 4]8
[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]8
[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]9
[1, 2, 3, 4]9# Python program to demonestrate to1
Output:
['code', 'geeks', 'ide', 'practice']Example 3: Sort the List in Descending Order
Here, we are sorting the list of numbers in Descending order, the same will be for alphabets(Z-A, z-a).
Python3
# Python program to demonestrate to
# Python program to demonestrate to3
# Python program to demonestrate to4
numbers= [[1, 2, 3, 4]0[1, 2, 3, 4]1[1, 2, 3, 4]2[1, 2, 3, 4]1[1, 2, 3, 4]4[1, 2, 3, 4]1[1, 2, 3, 4]6[1, 2, 3, 4]7
# sorting numbers in Ascending Order6
# sorting numbers in Ascending Order7= # sorting numbers in Ascending Order9numbers0
[1, 2, 3, 4]9numbers2
Output:
[4, 3, 2, 1]Example 4: Sorting using User-defined Order
In this example, we are sorting elements using the function based by passing the function to the key parameter of the sort() function.
Python3
numbers3
numbers4
numbers5 numbers6
numbers7numbers8 numbers9[1, 2, 3, 4]0[1, 2, 3, 4]7
=2
=3
=4= =6[1, 2, 3, 4]0[1, 2, 3, 4]1[1, 2, 3, 4]6[0[1, 2, 3, 4]2[1, 2, 3, 4]1[1, 2, 3, 4]2[0[1, 2, 3, 4]0[1, 2, 3, 4]1[1, 2, 3, 4]0[8