Open In App
Related Articles

Python Program to Sort Matrix Rows According to Primary and Secondary Indices

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given Matrix, the task here is to write a Python program to sort rows based on primary and secondary indices. First using primary indices the rows will be arranged based on the element each row has at the specified primary index. Now if two rows have the same element at the given primary index, sorting will be performed again using secondary index.

Input : test_list = [[2, 5, 7, 4], [8, 1, 3, 10], [9, 1, 9, 4], [10, 1, 1, 4]], pri, sec = 3, 2

Output : [[10, 1, 1, 4], [2, 5, 7, 4], [9, 1, 9, 4], [8, 1, 3, 10]]

Explanation : Sorted by 3rd index, and then by 2nd index in case of equal elements in 3rd index.

Input : test_list = [[2, 5, 7, 4], [8, 1, 3, 10], [9, 1, 9, 4], [10, 1, 1, 4]], pri, sec = 1, 2

Output : [[10, 1, 1, 4], [8, 1, 3, 10], [9, 1, 9, 4], [2, 5, 7, 4]]

Explanation : Sorted by 1st index, and then by 2nd index in case of equal elements in 1st index.

Method 1 : Using sort() and lambda 

This performs in-place sorting using sort() and lambda function is used to perform sorting using sort order defined in order in key from lambda function.

Program:

Python3

# initializing list
test_list = [[2, 5, 7, 4], [8, 1, 3, 10], [9, 1, 9, 4], [10, 1, 1, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing pri, sec
pri, sec = 3, 2
 
# inplace sorting using sort()
test_list.sort(key=lambda ele: (ele[pri], ele[sec]))
 
# printing result
print("Matrix after sorting : " + str(test_list))

                    

Output:

The original list is : [[2, 5, 7, 4], [8, 1, 3, 10], [9, 1, 9, 4], [10, 1, 1, 4]]

Matrix after sorting : [[10, 1, 1, 4], [2, 5, 7, 4], [9, 1, 9, 4], [8, 1, 3, 10]]

Time Complexity: O(nlogn+mlogm)
Auxiliary Space: O(k)

Method 2 : Using sorted() and itemgetter()

In this, we perform task of sorting using sorted() and itemgetter() is used to perform task of getting the index as required. 

Program:

Python3

import operator
 
# initializing list
test_list = [[2, 5, 7, 4], [8, 1, 3, 10], [9, 1, 9, 4], [10, 1, 1, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing pri, sec
pri, sec = 3, 2
 
# inplace sorting using sort()
res = sorted(test_list, key=operator.itemgetter(pri, sec))
 
# printing result
print("Matrix after sorting : " + str(res))

                    

Output:

The original list is : [[2, 5, 7, 4], [8, 1, 3, 10], [9, 1, 9, 4], [10, 1, 1, 4]]

Matrix after sorting : [[10, 1, 1, 4], [2, 5, 7, 4], [9, 1, 9, 4], [8, 1, 3, 10]]

Time Complexity: O(nlogn) where n is the number of elements in the list “test_list”. The sorted and itemgetter function is used to perform the task and it takes O(nlogn) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”. 



Last Updated : 08 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads