[Numpy-discussion] lexsort

Eleanor e.howick at irl.cri.nz
Tue May 6 17:50:50 EDT 2008


>>> a = numpy.array([[1,2,6], [2,2,8], [2,1,7],[1,1,5]])
>>> a
array([[1, 2, 6],
       [2, 2, 8],
       [2, 1, 7],
       [1, 1, 5]])
>>> indices = numpy.lexsort(a.T)
>>> a.T.take(indices,axis=-1).T
array([[1, 1, 5],
       [1, 2, 6],
       [2, 1, 7],
       [2, 2, 8]])


The above does what I want, equivalent to sorting on column A then
column B in Excel, but al the transposes are ungainly. I've stared at it a while
but can't come up with a more elegant solution. Any ideas?

cheers Eleanor




More information about the NumPy-Discussion mailing list