From your example, it's not clear if the pairs are sorted together or separately
Sort pairs together
>>> matrix = [[-1, -1], [-1, -1], [4, 2], [4, 1]]
>>> sorted(matrix)
[[-1, -1], [-1, -1], [4, 1], [4, 2]]
Sort pairs separately
You can use zip
to transpose your matrix, sorted
to sort the rows and zip
to transpose your sorted rows back into the matrix:
>>> zip(*matrix)
[(-1, -1, 4, 4), (-1, -1, 2, 1)]
>>> [sorted(row) for row in zip(*matrix)]
[[-1, -1, 4, 4], [-1, -1, 1, 2]]
>>> zip(*[sorted(row) for row in zip(*matrix)])
[(-1, -1), (-1, -1), (4, 1), (4, 2)]
With another matrix as input, the difference becomes clearer:
>>> matrix = [[1, 2], [3, 1], [2, 3]]
>>> sorted(matrix)
[[1, 2], [2, 3], [3, 1]]
>>> zip(*[sorted(row) for row in zip(*matrix)])
[(1, 1), (2, 2), (3, 3)]