Python - Sort Strings by Maximum ASCII value
Last Updated :
08 Feb, 2024
Given strings list, perform sort by Maximum Character in String.
Input : test_list = ["geeksforgeeks", "is", "best", "cs"]
Output : ["geeksforgeeks", "is", "cs", "best"]
Explanation : s = s = s < t, sorted by maximum character.
Input : test_list = ["apple", "is", "fruit"]
Output : ["apple", "is", "fruit"]
Explanation : p < s < t, hence order retained after sorting by max. character.
Method #1 : Using sort() + max()
In this, sorting is performed using sort() and max() is used to get maximum character from Strings.
Python3
# Python3 code to demonstrate working of
# Sort Strings by Maximum Character
# Using sort() + max()
# get maximum character fnc.
def get_max(sub):
# returns maximum character
return ord(max(sub))
# initializing list
test_list = ["geeksforgeeks", "is", "best", "for", "cs"]
# printing original lists
print("The original list is : " + str(test_list))
# performing sorting
test_list.sort(key=get_max)
# printing result
print("Sorted List : " + str(test_list))
OutputThe original list is : ['geeksforgeeks', 'is', 'best', 'for', 'cs']
Sorted List : ['for', 'geeksforgeeks', 'is', 'cs', 'best']
Method #2 : Using sorted() + lambda + max()
In this, we perform task of sorting using sorted(), lambda and max() are used to input logic of getting maximum character.
Python3
# Python3 code to demonstrate working of
# Sort Strings by Maximum Character
# Using sorted() + lambda + max()
# initializing list
test_list = ["geeksforgeeks", "is", "best", "for", "cs"]
# printing original lists
print("The original list is : " + str(test_list))
# performing sorting using sorted()
# lambda function provides logic
res = sorted(test_list, key=lambda sub: ord(max(sub)))
# printing result
print("Sorted List : " + str(res))
OutputThe original list is : ['geeksforgeeks', 'is', 'best', 'for', 'cs']
Sorted List : ['for', 'geeksforgeeks', 'is', 'cs', 'best']
Time Complexity: O(n) -> built-in functions like max takes O(n)
Auxiliary Space: O(n)
Method 3: Using heapq.nlargest():
In this approach, we can use the heapq.nlargest() function to find the n largest elements of a list based on a given key function. We can pass k=len(test_list) to get all the elements of the list, and use a lambda function to return the maximum ASCII value of each string.
Python3
import heapq
test_list = ["geeksforgeeks", "is", "best", "cs"]
sorted_list = heapq.nlargest(len(test_list), test_list, key=lambda s: max(map(ord, s)))
print(sorted_list) # Output: ['geeksforgeeks', 'is', 'cs', 'best']
Output['best', 'geeksforgeeks', 'is', 'cs']
Time Complexity: O(nmlog(k)), where n is the number of strings in the list, m is the length of the longest string, and k is the number of largest elements to be found. In this case, k is equal to len(test_list), so the time complexity is O(nmlog(n)).
Auxiliary Space: O(k), where k is the number of largest elements to be found. In this case, k is equal to len(test_list), so the space complexity is O(n).
Method #4: Using a custom function with list comprehension
Python3
# Python3 code to demonstrate working of
# Sort Strings by Maximum Character
# Using a custom function with list comprehension
# initializing list
test_list = ["geeksforgeeks", "is", "best", "for", "cs"]
# defining custom function to get maximum character ASCII value
def max_char_ascii(s):
return ord(max(s))
# using list comprehension to create list of tuples
lst = [(s, max_char_ascii(s)) for s in test_list]
# sorting list of tuples based on second element (maximum character ASCII value)
res = sorted(lst, key=lambda x: x[1])
# extracting first element of each tuple (original string)
res = [t[0] for t in res]
# printing result
print("Sorted List : " + str(res))
OutputSorted List : ['for', 'geeksforgeeks', 'is', 'cs', 'best']
Time complexity: O(n log n) (due to the use of the sorted() function)
Auxiliary space: O(n) (for the lst list of tuples)
Similar Reads
Python - Sort Strings by maximum frequency character Given a string, the task is to write a Python program to perform sort by maximum occurring character. Input : test_list = ["geekforgeeks", "bettered", "for", "geeks"] Output : ['for', 'geeks', 'bettered', 'geekforgeeks'] Explanation : 1 < 2 < 3 < 4, is ordering of maximum character occurren
3 min read
Python - Sort Matrix by Maximum String Length Given a matrix, perform row sort basis on the maximum length of the string in it. Input : test_list = [['gfg', 'best'], ['geeksforgeeks'], ['cs', 'rocks'], ['gfg', 'cs']] Output : [['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks'], ['geeksforgeeks']] Explanation : 3 < 4 < 5 < 13, maximum leng
3 min read
Python program to list Sort by Number value in String Given a List of strings, the task is to write a Python program to sort list by the number present in the Strings. If no number is present, they will be taken to the front of the list. Input : test_list = ["gfg is 4", "all no 1", "geeks over 7 seas", "and 100 planets"] Output : ['all no 1', 'gfg is 4
6 min read
Python Program to Sort a String Sorting strings in Python is a common and important task, whether we need to organize letters alphabetically or systematically handle text data. In this article, we will explore different methods to sort a string starting from the most efficient to the least.Using sorted with join()sorted() function
2 min read
Python Program to Sort a String Sorting strings in Python is a common and important task, whether we need to organize letters alphabetically or systematically handle text data. In this article, we will explore different methods to sort a string starting from the most efficient to the least.Using sorted with join()sorted() function
2 min read
Sort Numeric Strings in a List - Python We are given a list of numeric strings and our task is to sort the list based on their numeric values rather than their lexicographical order. For example, if we have: a = ["10", "2", "30", "4"] then the expected output should be: ["2", "4", "10", "30"] because numerically, 2 < 4 < 10 < 30.
2 min read