Sometimes, while working with records, we can have a problem in which we need to perform sort operation on one of the columns, and on other column, if equal elements, opposite sort. This kind of problem can occur as application in many domains such as web development. Lets discuss certain ways in which this problem can be solved.
Input : test_list = [(6, 7), (6, 5), (6, 4), (7, 10)]
Output : [(7, 10), (6, 4), (6, 5), (6, 7)]Input : test_list = [(10, 7), (8, 5)]
Output : [(10, 7), (8, 5)]
Method #1 : Using sorted() + lambda
The combination of above functions can offer one of the ways to solve this problem. In this, we perform sort using sorted() and order and column manipulation is handled by lambda function.
Python3
# Python3 code to demonstrate working of # Multiple Column Sort in Tuples # Using sorted() + lambda # initializing list test_list = [( 6 , 7 ), ( 6 , 5 ), ( 1 , 4 ), ( 8 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) # Multiple Column Sort in Tuples # Using sorted() + lambda res = sorted (test_list, key = lambda sub: ( - sub[ 0 ], sub[ 1 ])) # printing result print ( "The sorted records : " + str (res)) |
The original list is : [(6, 7), (6, 5), (1, 4), (8, 10)] The sorted records : [(8, 10), (6, 5), (6, 7), (1, 4)]
Time Complexity: O(nlogn) where n is the number of elements in the in the list “test_list”. The sorted() + lambda is used to perform the task and it takes O(nlogn) time.
Auxiliary Space: O(1) additional space is not required
Method #2 : Using itemgetter() + sorted()
This is yet another way in which this task can be performed. In this, we perform the task required for lambda function using itemgetter().
Python3
# Python3 code to demonstrate working of # Multiple Column Sort in Tuples # Using itemgetter() + sorted() from operator import itemgetter # initializing list test_list = [( 6 , 7 ), ( 6 , 5 ), ( 1 , 4 ), ( 8 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) # Multiple Column Sort in Tuples # Using itemgetter() + sorted() res = sorted (test_list, key = itemgetter( 1 )) res = sorted (res, key = itemgetter( 0 ), reverse = True ) # printing result print ( "The sorted records : " + str (res)) |
The original list is : [(6, 7), (6, 5), (1, 4), (8, 10)] The sorted records : [(8, 10), (6, 5), (6, 7), (1, 4)]
Method #3:Using listcomprehension
- Define the original list of tuples.
- Define the key function for sorting the tuples. The key function should take a tuple as input and return a tuple of values that will be used for sorting the original list of tuples.
- Call the sorted() function with the original list of tuples and the key function as arguments.
- Store the sorted list of tuples in a variable.
- Print the sorted list of tuples.
Python3
# initializing list test_list = [( 6 , 7 ), ( 6 , 5 ), ( 1 , 4 ), ( 8 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) # Multiple Column Sort in Tuples # Using list comprehension res = sorted (test_list, key = lambda sub: ( - sub[ 0 ], sub[ 1 ])) # Alternatively, using list comprehension res = [(x, y) for (x, y) in sorted (test_list, key = lambda sub: ( - sub[ 0 ], sub[ 1 ]))] # printing result print ( "The sorted records : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original list is : [(6, 7), (6, 5), (1, 4), (8, 10)] The sorted records : [(8, 10), (6, 5), (6, 7), (1, 4)]
Time complexity:
Sorting the original list of tuples using sorted() takes O(N log N) time complexity, where N is the number of tuples in the list.
The lambda function takes O(1) time complexity as it performs a constant number of operations for each tuple.
Therefore, the overall time complexity for performing the multiple column sort using sorted() and lambda function is O(N log N).
Space complexity:
Sorting the original list of tuples using sorted() requires O(N) space complexity to store the sorted list of tuples.
The lambda function requires O(1) space complexity to store the tuple of values for sorting.
Therefore, the overall space complexity for performing the multiple column sort using sorted() and lambda function is O(N).