Sorting on a List of Strings in Python

Write a python program to perform sorting on a given list of strings, on the basis of length of strings. That is, the smallest-length string should be the first string in the list and the largest-length string should be the last string in the sorted list.

PYTHON PROGRAMMING

Raki Adhikary

1/30/20231 min read

def sort_by_length(strings):

for i in range(len(strings)):

min_idx = i

for j in range(i+1, len(strings)):

if len(strings[j]) < len(strings[min_idx]):

min_idx = j

strings[i], strings[min_idx] = strings[min_idx], strings[i]

# Example list of strings

strings = ["apple", "banana", "orange", "kiwi", "grape"]

# Sort the list based on string lengths

sort_by_length(strings)

# Print the sorted list

print("Sorted list of strings by length:")

for string in strings:

print(string)

Your Code is here