Home > Back-end >  How to sort first row in numpy array and have that sort the second row?
How to sort first row in numpy array and have that sort the second row?

Time:09-17

I have an array that looks like this

array = [[3,5,4,2,1,6,8],[23,44,52,1,23,22,58]]

How do I sort the first row and have it change the result of the second row as well? This is what I mean?

new_array = [[1,2,3,4,5,6,8],[23,1,23,52,44,22,58]]

CodePudding user response:

You can use argsort, array[0].argsort() gives the indices that will sort the first row and then use the indices to reorder all columns:

array[:, array[0].argsort()]
array([[ 1,  2,  3,  4,  5,  6,  8],
       [23,  1, 23, 52, 44, 22, 58]])
  • Related