Python | Ways to sort list of strings in case-insensitive manner
Last Updated :
26 Apr, 2023
Given a list of strings, A task is to sort the strings in a case-insensitive manner. Given below are a few methods to solve the task.
Method #1: Using casefold()
Python3
ini_list = [ 'akshat' , 'garg' , 'GeeksForGeeks' , 'Alind' ,
'SIngh' , 'manjeet' , 'Munich' ]
res1 = sorted (ini_list)
print ( "Case-sensitive sorted list" , str (res1))
res2 = sorted (ini_list, key = lambda s: s.casefold())
print ( "Case-insensitive sorted list" , str (res2))
|
Output
Case-sensitive sorted list ['Alind', 'GeeksForGeeks', 'Munich', 'SIngh', 'akshat', 'garg', 'manjeet']
Case-insensitive sorted list ['akshat', 'Alind', 'garg', 'GeeksForGeeks', 'manjeet', 'Munich', 'SIngh']
Time Complexity: O(nlogn)
Space Complexity: O(n)
Method #2: Using lower()
Python3
ini_list = [ 'akshat' , 'garg' , 'GeeksForGeeks' , 'Alind' ,
'SIngh' , 'manjeet' , 'Munich' ]
ini_list.sort()
print ( "Case-sensitive sorted list" , str (ini_list))
ini_list.sort(key = lambda x: x.lower())
print ( "Case-insensitive sorted list" , str (ini_list))
|
Output
Case-sensitive sorted list ['Alind', 'GeeksForGeeks', 'Munich', 'SIngh', 'akshat', 'garg', 'manjeet']
Case-insensitive sorted list ['akshat', 'Alind', 'garg', 'GeeksForGeeks', 'manjeet', 'Munich', 'SIngh']
Time Complexity: O(nlogn)
Space Complexity: O(n)
Method #3: Using sort() method and str.lower() method:
Approach:
- In this example, we have a strings list containing five strings with different casing.
- We pass this list as an argument to the sort_strings_case_insensitive() function.
- Now store the result in the sorted_strings variable.
- Finally, we print the sorted_strings variable to see the resulting sorted list in a case-insensitive manner.
Example:
Python3
def sort_strings_case_insensitive(strings):
strings.sort(key = str .lower)
return strings
strings = [ "apple" , "Banana" , "CHERRY" , "durian" , "EGGFRUIT" ]
sorted_strings = sort_strings_case_insensitive(strings)
print (sorted_strings)
|
Output
['apple', 'Banana', 'CHERRY', 'durian', 'EGGFRUIT']
Time Complexity: O(n log n), where n is the length of the list
Auxiliary Space: O(1)
Method #4: Use the merge sort algorithm with a key function to sort the list in a case-insensitive manner. The key function takes a string and returns its lowercase version, which will be used to compare the strings during the sorting process.
Here are the steps to implement this method:
- Define a merge_sort function that takes a list and a key function as arguments.
- If the length of the list is less than or equal to 1, return the list.
- Divide the list into two halves and recursively call merge_sort on each half.
- Merge the two sorted halves by comparing the elements using the key function.
- Return the merged list.
- Call the merge_sort function with the initial list and the key function as lambda x: x.lower().
- Print the sorted list.
Python3
def merge_sort(lst, key):
if len (lst) < = 1 :
return lst
mid = len (lst) / / 2
left = merge_sort(lst[:mid], key)
right = merge_sort(lst[mid:], key)
return merge(left, right, key)
def merge(left, right, key):
result = []
i, j = 0 , 0
while i < len (left) and j < len (right):
if key(left[i]) < = key(right[j]):
result.append(left[i])
i + = 1
else :
result.append(right[j])
j + = 1
result + = left[i:]
result + = right[j:]
return result
ini_list = [ 'akshat' , 'garg' , 'GeeksForGeeks' , 'Alind' ,
'SIngh' , 'manjeet' , 'Munich' ]
res = merge_sort(ini_list, key = lambda x: x.lower())
print ( "Case-insensitive sorted list:" , res)
|
Output
Case-insensitive sorted list: ['akshat', 'Alind', 'garg', 'GeeksForGeeks', 'manjeet', 'Munich', 'SIngh']
Time complexity: O(n log n)
Auxiliary space: O(n).
Method #5: Comparing each element and placing at their correct position.
Sort the list in a case-insensitive manner by comparing the lowercase versions of the strings using the lower() method.
Python3
ini_list = [ 'akshat' , 'garg' , 'GeeksForGeeks' , 'Alind' ,
'SIngh' , 'manjeet' , 'Munich' ]
for i in range ( len (ini_list)):
for j in range (i + 1 , len (ini_list)):
if ini_list[j].lower() < ini_list[i].lower():
ini_list[i], ini_list[j] = ini_list[j], ini_list[i]
print ( "Case-sensitive sorted list" , str ( sorted (ini_list)))
print ( "Case-insensitive sorted list using bubble sort" , str (ini_list))
|
Output
Case-sensitive sorted list ['Alind', 'GeeksForGeeks', 'Munich', 'SIngh', 'akshat', 'garg', 'manjeet']
Case-insensitive sorted list using bubble sort ['akshat', 'Alind', 'garg', 'GeeksForGeeks', 'manjeet', 'Munich', 'SIngh']
Time complexity: O(n * n)
Auxiliary space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...