Python – Reverse Sort a String

17 Jun, 2022
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 stringtest_string = "geekforgeeks" # printing original stringprint("The original string : " + str(test_string)) # using join() + sorted() + reverse# Sorting a stringres = ''.join(sorted(test_string, reverse = True))     # print resultprint("String after reverse sorting : " + str(res))
The original string : geekforgeeks
String after reverse sorting : srokkggfeeee

 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 stringtest_string = "geekforgeeks" # printing original stringprint("The original string : " + str(test_string)) # using sorted() + reduce() + lambda# Reverse Sort a Stringres = reduce(lambda x, y: x + y, sorted(test_string, reverse = True))     # print resultprint("String after reverse sorting : " + str(res))
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 stringtest_string = "geekforgeeks" # printing original stringprint("The original string : " + str(test_string)) # using list() + sort() + join# To Sort and reverse a Stringtemp = list(test_string)temp.sort(reverse = True)res = "".join(temp)     # print resultprint("String after reverse sorting : " + res)
The original string : geekforgeeks
String after reverse sorting : srokkggfeeee

satyam00so
Python string-programs
Python-sort
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary