Sorting a 2D numpy array by multiple axes

Posted by perimosocordiae on Stack Overflow See other posts from Stack Overflow or by perimosocordiae
Published on 2010-04-24T23:39:55Z Indexed on 2010/04/24 23:43 UTC
Read the original article Hit count: 175

Filed under:
|
|

I have a 2D numpy array of shape (N,2) which is holding N points (x and y coordinates). For example:

array([[3, 2],
       [6, 2],
       [3, 6],
       [3, 4],
       [5, 3]])

I'd like to sort it such that my points are ordered by x-coordinate, and then by y in cases where the x coordinate is the same. So the array above should look like this:

array([[3, 2],
       [3, 4],
       [3, 6],
       [5, 3],
       [6, 2]])

If this was a normal Python list, I would simply define a comparator to do what I want, but as far as I can tell, numpy's sort function doesn't accept user-defined comparators. Any ideas?

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy