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?
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]])
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.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.)
l.sort()does the trick for normal listsl.sort()convertsl = [[2,1], [0,2]]into[[0, 2], [2, 1]]only for a normal list and not for an numpy array