I'm trying to write a function that sorts a 2D List with 3 columns and multiple rows: The function first sorts in ascending order by the number in the 3rd column, then with all the elements that have the same "3rd column number", sort them by the 1st column in ascending order.
For example, I have a sample 2d list like:
lst = [[9,3,4],
[8,4,2],
[4,2,4]]
After sorting it by the "3rd column" in ascending order first, the list becomes
lst = [[8,4,2],
[9,3,4],
[4,2,4]]
Then, sort all rows with the same 3rd column by the "1st column" in ascending order:
lst = [[8,4,2],
[4,2,4],
[9,3,4]]
[4,2,4]
was swapped with [9,3,4]
because 4 (1st element in the 2nd row of lst) < 9 (1st element in the 3rd row of lst). Also, [4,2,4]
and [9,3,4]
both have the same 3rd element, which is 4.
How can I write a function to do this without using Numpy? I found an answer similar to my question here, but that is for tuples, not for 2d lists. Does the method for sorting tuples mentioned in the link also work for 2D lists?
Thanks!