Sorting a python array/recarray by column

Use data[np.argsort(data[:, 0])] where the 0 is the column index on which to sort:

In [27]: import numpy as np

In [28]: data = np.array([[5,2], [4,1], [3,6]])

In [29]: col = 0

In [30]: data=data[np.argsort(data[:,col])]
Out[30]: 
array([[3, 6],
       [4, 1],
       [5, 2]])

Leave a Comment