Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Reverse Sort a String

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 07 Mar, 2023
Improve Article
Save Article
Like Article

Sorting has always been quite a popular utility with lots of applications everywhere, where Python language has opted. Python in its language offers a sort function to perform this task. But due to fact that not all the containers in Python are mutable, such as string, the sort function doesn’t work as it inplace tries to sort and immutability stops this. Let’s discuss certain ways in which a string can be sorted in a reverse way.

Method #1 : join() + sorted() + reverse key: The combination of above functions can potentially solve this particular problem. This task is performed in 2 steps in which the first step we get the reverse sorted list of characters and then we join the result to get the resultant sorted string. 

Python3




# Python3 code to demonstrate
# Reverse Sort a String
# using join() + sorted() + reverse
 
# initializing string
test_string = "geekforgeeks"
 
# printing original string
print("The original string : " + str(test_string))
 
# using join() + sorted() + reverse
# Sorting a string
res = ''.join(sorted(test_string, reverse = True))
     
# print result
print("String after reverse sorting : " + str(res))


Output

The original string : geekforgeeks
String after reverse sorting : srokkggfeeee

Time Complexity: O(nlogn)
Auxiliary Space : O(n), where n is the number of characters in string.

 Method #2 : Using sorted() + reduce() + lambda: This particular task can also be performed using the combination of above 3 functions. Here we join the resultant reverse sorted list of characters using the lambda function joined by the reduce function. Works only for Python2. 

Python




# Python code to demonstrate
# Reverse Sort a String
# using sorted() + reduce() + lambda
 
# initializing string
test_string = "geekforgeeks"
 
# printing original string
print("The original string : " + str(test_string))
 
# using sorted() + reduce() + lambda
# Reverse Sort a String
res = reduce(lambda x, y: x + y, sorted(test_string, reverse = True))
     
# print result
print("String after reverse sorting : " + str(res))


Output

The original string : geekforgeeks
String after reverse sorting : srokkggfeeee

 Method#3 : Using list() + sort() + join(): This problem can also be solve using the combination of above 3 methods. Here we convert the string to a list and sort this list with the sort function of the list in reverse order and after that join the result into a string.

Python3




# Python code to demonstrate
# To Sort and Reverse a String
# using list() + sort() + join()
 
# initializing string
test_string = "geekforgeeks"
 
# printing original string
print("The original string : " + str(test_string))
 
# using list() + sort() + join
# To Sort and reverse a String
temp = list(test_string)
temp.sort(reverse = True)
res = "".join(temp)
     
# print result
print("String after reverse sorting : " + res)


Output

The original string : geekforgeeks
String after reverse sorting : srokkggfeeee

Time Complexity: O(nlogn)
Auxiliary space: O(n)

method#4: Using map() function and lambda expression

Step-by-step algorithm:

  1. Initialize the list with elements containing the substring to be replaced.
  2. Print the original list.
  3. Use the map() function with lambda expression to replace the substring in each element of the list.
  4. Convert the map object to list and store it in a variable.
  5. Print the modified list.

Python3




# initializing list
test_list = ['4', 'kg', 'butter', 'for', '40', 'bucks']
 
# print original list
print("Original List : ", test_list)
 
# replace substring using map() function and lambda expression
res = list(map(lambda x: x.replace('4', '1'), test_list))
 
# print modified list
print("Modified List : ", res)


Output

Original List :  ['4', 'kg', 'butter', 'for', '40', 'bucks']
Modified List :  ['1', 'kg', 'butter', 'for', '10', 'bucks']

Time complexity: The time complexity of the map() function is O(n), where n is the number of elements in the list. The time complexity of the replace() function is O(m), where m is the length of the string. Therefore, the overall time complexity of the code is O(n*m).

Auxiliary space complexity: The space complexity of the code depends on the size of the list and the size of the substring to be replaced. The map() function returns a map object, which is converted to a list and stored in a variable. Therefore, the space complexity of the code is O(n), where n is the number of elements in the list.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!