0

How can I sort a n x 2 Numpy array in Python?

I know of the np.sort function but can only get it to sort 1 column independently of all the others.

I want the second columns' entries to follow that of the first.

Jonny
  • 1,270
  • 5
  • 19
  • 31
  • Thanks @fhdrsdg I think you're right, but I cant figure out the syntax. If I have the array a = np.array([[3,2],[4,5],[1,8],[9,4],[7,3]]) , how would I sort it according to the first column? – Jonny Oct 24 '14 at 09:19
  • 1
    No sorry I was wrong. Use `a[a[:,0].argsort()]` (found in [this question](http://stackoverflow.com/questions/2828059/sorting-arrays-in-numpy-by-column)). The 0 indicates the first column. – fhdrsdg Oct 24 '14 at 09:23
  • 1
    @Jonny you probably want `np.lexsort()` – Saullo G. P. Castro Oct 24 '14 at 09:49
  • Thank you @SaulloCastro, fhdrsdg's anwer also does the trick! – Jonny Oct 24 '14 at 10:08
  • This is a form of lexical ordering, so @SaulloCastro's comment is the way to go, [doc here](http://docs.scipy.org/doc/numpy/reference/generated/numpy.lexsort.html). – eickenberg Oct 24 '14 at 11:37
  • Ok good to know, thanks! Can you please indicate how lexsort should be implemented to achieve what I am looking for? The documentation doesn't indicate it very clearly for me. – Jonny Oct 24 '14 at 11:38

0 Answers0