An example numpy.lexsort() application for sorting array rows and working with relationships in the first column. Note that lexsort efficiently sorts the columns and starts from the last column, so you need to reverse the lines of a , then transpose before lexsort and finally transpose the result (you might think this should be easier, but hey!):
In [1]: import numpy as np In [2]: a = np.array([[1,2,3,4],[1,0,4,1],[0,4,1,1]]) In [3]: a[np.lexsort(np.flip(a, axis=1).T).T] Out[3]: array([[0, 4, 1, 1], [1, 0, 4, 1], [1, 2, 3, 4]]) In [4]: a Out[4]: array([[1, 2, 3, 4], [1, 0, 4, 1], [0, 4, 1, 1]])
Thanks @Paul for suggesting using lexsort .
drevicko
source share