;

Python Program to Sort a String List in an Ascending Order


Tutorialsrack 04/05/2020 Python

In this Python Program, we will learn how to sort the string list in ascending order. In this program, for sorting a list we used the python built-in function such as sort(), sorted(), without using the sorting function, and using the quicksort algorithm.

Here is the source code of the program to sort a string list in ascending order.

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

In this program, we used the python built-in function sort() to sort the string list in ascending order by default. The sort() function takes two parameters: 

reverse = the first parameter is optional.if reverse = True then it returns a sorted list in descending order.

Key = second Parameter. It is also optional and this function used to specify the sorting criteria(s)

Python Program to Sort a String List in an Ascending Order Using sort() function
# Python Program to Sort a String List in an Ascending Order Using sort() function

str_List = ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']

# Print the String List
print("String List Before Sorting is: \n",str_List)

str_List.sort()

print("\nString List After Sorting in Ascending Order is: \n",str_List)
Output

String List Before Sorting is: 

 ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']

 

String List After Sorting in Ascending Order is: 

 ['Apple', 'Banana', 'Grapes', 'Guava', 'Orange', 'Peach', 'Strawberry']

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

In this program, we used the python built-in sorted() function to sort the list in ascending order. Python lists have a built-in sorted() function that builds a new sorted list from an iterable. sorted() have a key parameter to specify a function to be called on each list element prior to making comparisons.

Python Program to Sort a String List in an Ascending Order Using sorted() function
# Python Program to Sort a String List in an Ascending Order Using sorted() function

str_List = ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']

# Print the String List
print("String List Before Sorting is: \n",str_List)

newstr_List = sorted(str_List)

print("\nString List After Sorting in Ascending Order is: \n",newstr_List)
Output

String List Before Sorting is: 

 ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']

 

String List After Sorting in Ascending Order is: 

 ['Apple', 'Banana', 'Grapes', 'Guava', 'Orange', 'Peach', 'Strawberry']

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

Python Program to Sort a String List in an Ascending Order Using For Loop
# Python Program to Sort a String List in an Ascending Order Using For Loop

str_List = ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']

# Print the String 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 Ascending Order is: \n",str_List)
Output

String List Before Sorting is: 

 ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']

 

String List After Sorting in Ascending Order is: 

 ['Apple', 'Banana', 'Grapes', 'Guava', 'Orange', 'Peach', 'Strawberry']

Program 4: Python Program to Sort a String List in an Ascending 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 an Ascending Order Using QuickSort Algorithm
# Python Program to Sort a String List in an Ascending Order Using QuickSort Algorithm
 
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 string List
print("String List Before Sorting is: \n",str_List)

# Print the String List After Sorting
print("\nString List After Sorting in Ascending Order is: \n",quicksort(str_List))
Output

String List Before Sorting is: 

 ['Orange', 'Banana', 'Strawberry', 'Apple', 'Grapes', 'Peach', 'Guava']

 

String List After Sorting in Ascending Order is: 

 ['Apple', 'Banana', 'Grapes', 'Guava', 'Orange', 'Peach', 'Strawberry']


Related Posts



Comments

Recent Posts
Tags