I haven't found this answer on SO, so I am sharing it here:
Question: How do you emulate sortrows functionality in matlab when there are multiple sorting keys? In matlab, this looks like e.g.:
sortrows(x,[3,-4])
which sorts first by the 3rd column and then by the second column.
If you were sorting by one column, you could use np.argsort
to find the indices of that column, and apply those indices. But how do you do it for multiple columns?
The syntax is quite unwieldy and looks weird, but the cleanest thing to do is np.lexsort
.
data = np.array([[3, 0, 0, .24],
[4, 1, 1, .41],
[2, 1, 1, .63],
[1, 1, 3, .38]]) #imagine rows of a spreadsheet
#now do sortrows(data,[3,-4])
ix = np.lexsort((data[:, 3][::-1], data[:, 2]))
#this yields [0, 2, 1, 3]
#note that lexsort sorts first from the last row, so sort keys are in reverse order
data[ix]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With