3

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?

Pedro Carvalho
  • 565
  • 1
  • 6
  • 26

1 Answers1

5

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]])
tom10
  • 67,082
  • 10
  • 127
  • 137
  • Do you know what the time complexity of this is? – Pedro Carvalho Jan 21 '15 at 20:38
  • @PedroCarvalho: For an h-by-w array, the worst case performance is `O(h*w*log(h))`, since it's a comparison sort on an input of length h where comparisons can take worst-case O(w) time. If rows usually differ in the first few columns, the expected performance will be `O(h*log(h))`, since comparisons will take expected O(1) time. – user2357112 May 25 '15 at 23:54
  • Also, rather than listing columns individually, you can do `x[np.lexsort(x.T[::-1])]`. (That's still way too many intermediate steps to just do a lexicographic sort, though. NumPy's sorting API seems to be put together in a really weird way.) – user2357112 May 25 '15 at 23:57