Red
Red

Reputation: 425

Lexicographic sort float array python

Okay, so, I have a 4x2 numpy ndarray, and I want to sort it lexicographically. That is, if I have the array

[[0,0],
[1,1],
[0,1],
[1,0]]

I want it to become

[[0,0],
[0,1],
[1,0],
[1,1]]

How do I do this?

Upvotes: 3

Views: 1678

Answers (1)

tom10
tom10

Reputation: 69242

You can use numpy's lexsort. Lexsort, though, sorts using the last column as the primary key. One way to get what you want is to specify the columns explicitly:

 x[np.lexsort((x[:,1], x[:,0]))]

 # array([[0, 0],
 #   [0, 1],
 #   [1, 0],
 #   [1, 1]])

Upvotes: 5

Related Questions