In this Python program, we will learn how to sort a string list in descending order. There are various ways to sort a string list in descending order in python. We can sort the list using python built-in functions such as using sort()
, sorted()
, using loops, and using the quicksort algorithm.
Here is the source code of the program to sort a string list in descending order.
In this program, we used the python built-in sort()
function to sort the list with the option reverse=True
. The sort()
function sorts the list and the reverse=True
reverses the string list in descending order.
# Python Program to Sort a String List in Descending Order Using sort() function
str_List = ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']
# Print the List
print("String List Before Sorting is: \n",str_List)
str_List.sort(reverse=True)
print("\nString List After Sorting in Descending Order is: \n",str_List)
String List Before Sorting is:
['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']
String List After Sorting in Descending Order is:
['Strawberry', 'Peach', 'Orange', 'Guava', 'Grapes', 'Banana', 'Apple']
In this program, we used the python built-in sorted() function with the option reverse=True. The sorted() function sort the string list and the parameter reverse=True reverse the string list in descending order.
# Python Program to Sort a String List in Descending Order Using sorted() function
str_List = ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']
# Print the List
print("String List Before Sorting is: \n",str_List)
newstr_List = sorted(str_List,reverse=True)
print("\nString List After Sorting in Descending Order is: \n",newstr_List)
String List Before Sorting is:
['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']
String List After Sorting in Descending Order is:
['Strawberry', 'Peach', 'Orange', 'Guava', 'Grapes', 'Banana', 'Apple']
# Python Program to Sort a String List in Descending Order Using For Loop
str_List = ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']
# Print the List
print("String List Before Sorting is: \n",str_List)
for item in range(len(str_List)-1, 0, -1):
for i in range(item):
if str_List[i] < str_List[i+1]:
str_List[i], str_List[i+1] = str_List[i+1], str_List[i]
print("\nString List After Sorting in Descending Order is: \n",str_List)
String List Before Sorting is:
['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']
String List After Sorting in Descending Order is:
['Strawberry', 'Peach', 'Orange', 'Guava', 'Grapes', 'Banana', 'Apple']
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively.
# Python Program to Sort a String List in Descending Order Using QuickSort Algorithm
# Define a Function
def quicksort(lst):
if not lst:
return []
return (quicksort([x for x in lst[1:] if x > lst[0]])
+ [lst[0]] +
quicksort([x for x in lst[1:] if x <= lst[0]]))
str_List = ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']
# Print the List
print("String List Before Sorting is: \n",str_List)
print("\nString List After Sorting in Descending Order is: \n",quicksort(str_List))
String List Before Sorting is:
['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']
String List After Sorting in Descending Order is:
['Strawberry', 'Peach', 'Orange', 'Guava', 'Grapes', 'Banana', 'Apple']
Comments