;

Python Program to Sort a String List in Descending Order


Tutorialsrack 04/05/2020 Python

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.

Program 1: Python Program to Sort a String List in Descending Order Using sort() function

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
# 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)
Output

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']

Program 2: Python Program to Sort a String List in Descending Order Using sorted() function

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
# 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)
Output

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']

Program 3: Python Program to Sort a String List in Descending Order Using For Loop

Python Program to Sort a String List in Descending Order Using For Loop
# 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)
Output

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']

Program 4: Python Program to Sort a String List in Descending Order Using QuickSort Algorithm

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
# 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))
Output

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']


Related Posts



Comments

Recent Posts
Tags