Python Program to Sort List Items in Descending Order

Write a Python program to sort list items in descending order. In this Python example, we used the list sort and reverse methods to sort the list in descending order.

intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))

for i in range(1, intlistTot + 1):
intlistvalue = int(input("Please enter the %d List Item = "  %i))
intlist.append(intlistvalue)

intlist.sort()
intlist.reverse()

print('List Items After Sorting in Descending Order')
print(intlist)

In this Python program, we used the nested for loops and temp variable to sort the list items in descending order.

intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))

for i in range(1, intlistTot + 1):
intlistvalue = int(input("Please enter the %d List Item = "  %i))
intlist.append(intlistvalue)

for i in range(len(intlist)):
for j in range(i + 1, len(intlist)):
if(intlist[i] < intlist[j]):
temp = intlist[i]
intlist[i] = intlist[j]
intlist[j] = temp

print('List Items After Sorting in Descending Order')
for i in range(len(intlist)):
print(intlist[i], end = '   ')
Total Number of List Items to Sort = 8
Please enter the 1 List Item = 17
Please enter the 2 List Item = 47
Please enter the 3 List Item = 12
Please enter the 4 List Item = 99
Please enter the 5 List Item = 55
Please enter the 6 List Item = 4
Please enter the 7 List Item = 60
Please enter the 8 List Item = 75
List Items After Sorting in Descending Order
99   75   60   55   47   17   12   4

Python program to sort list items in descending order using a while loop.

intlist = []
intlistTot = int(input("Total Number of List Items to Sort = "))

i = 1
while(i <= intlistTot):
intlistvalue = int(input("Please enter the %d List Item = "  %i))
intlist.append(intlistvalue)
i = i + 1

i = 0
while(i < len(intlist)):
j = i + 1
while j < len(intlist):
if(intlist[i] < intlist[j]):
temp = intlist[i]
intlist[i] = intlist[j]
intlist[j] = temp
j = j + 1
i = i + 1

print('List Items After Sorting in Descending Order')
i = 0
while(i < len(intlist)):
print(intlist[i], end = '   ')
i = i + 1
Total Number of List Items to Sort = 7
Please enter the 1 List Item = 19
Please enter the 2 List Item = 55
Please enter the 3 List Item = 89
Please enter the 4 List Item = 32
Please enter the 5 List Item = 47
Please enter the 6 List Item = 15
Please enter the 7 List Item = 7
List Items After Sorting in Descending Order
89   55   47   32   19   15   7