Sortrows with multiple sorting keys in numpy
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]