# Python – Sort Matrix by Maximum String Length

Last Updated : 10 Apr, 2023

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 lengths of strings, sorted increasingly.
Input : test_list = [['gfg', 'best'], ['cs', 'rocks'], ['gfg', 'cs']]
Output : [['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks']]
Explanation : 3 < 4 < 5 maximum lengths of strings, sorted increasingly.

### Method #1 : Using sort() + len() + max()

In this, in place sorting is performed using sort(), len() and max() to compute the maximum length of the string in each row to perform the sort.

## Python3

 # Python3 code to demonstrate working of # Sort Matrix by Maximum String Length # Using sort() + len() + max()     def max_len(row):       # getting Maximum length of string     return max([len(ele) for ele in row])     # initializing list test_list = [['gfg', 'best'], ['geeksforgeeks'],              ['cs', 'rocks'], ['gfg', 'cs']]   # printing original list print("The original list is : " + str(test_list))   # performing sort() test_list.sort(key=max_len)   # printing result print("Sorted Matrix : " + str(test_list))

Output:

The original list is : [[‘gfg’, ‘best’], [‘geeksforgeeks’], [‘cs’, ‘rocks’], [‘gfg’, ‘cs’]] Sorted Matrix : [[‘gfg’, ‘cs’], [‘gfg’, ‘best’], [‘cs’, ‘rocks’], [‘geeksforgeeks’]]

Time Complexity: O(n)

Auxiliary Space: O(n)

### Method #2 : Using sorted() + lambda + max() + len()

In this, we perform the task of filtering maximum using lambda function rather than the external function. The task of sorting is performed using sorted().

## Python3

 # Python3 code to demonstrate working of # Sort Matrix by Maximum String Length # Using sorted() + lambda + max() + len()   # initializing list test_list = [['gfg', 'best'], ['geeksforgeeks'],              ['cs', 'rocks'], ['gfg', 'cs']]   # printing original list print("The original list is : " + str(test_list))   # performing logic using lambda fnc. res = sorted(test_list, key=lambda row: max([len(ele) for ele in row]))   # printing result print("Sorted Matrix : " + str(res))

Output:

The original list is : [[‘gfg’, ‘best’], [‘geeksforgeeks’], [‘cs’, ‘rocks’], [‘gfg’, ‘cs’]] Sorted Matrix : [[‘gfg’, ‘cs’], [‘gfg’, ‘best’], [‘cs’, ‘rocks’], [‘geeksforgeeks’]]

Time Complexity: O(n)

Auxiliary Space: O(n)

### Using a function with sort():

Approach:

In this approach, we define a function that takes a sublist and returns the maximum length of the strings in that sublist. We use this function as the key parameter in the sort() function to sort the list of sublists by the maximum length of their strings.

## Python3

 def max_string_length(sublist):     return max(len(s) for s in sublist)   test_list = [['gfg', 'best'], ['cs', 'rocks'], ['gfg', 'cs']] test_list.sort(key=max_string_length) print(test_list)

Output

[['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks']]

Time Complexity: O(n log n) – where n is the length of the list of sublists
Space Complexity: O(1) – since we modify the original list in place

Previous
Next