I have looked far and wide for code for fast sorting of n dimensional arrays by the first element, for example if I had the array:

ray = [[1,2,3,7,5][10,11,12,13,14]]

I would want it to come out as ray = [[1,2,3,5,7][10,11,12,14,13]]

There are several ways to do this. One is
zipped = zip(*ray)
zipped.sort()
ray = zip(*zipped)

but this is extremely slow. Numpy has a much faster way to do it, but it wasn't immediately apparent.

if the above were a numpy array you could simply do the following: indexes = numpy.argsort(ray[0]) for n in xrange(len(ray)) ray[n] = ray[n][indexes]

I did a time test of the two methods below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ```
rows, cols = 3, 100000
A = np.random.randn(rows, cols)
B = [list(n) for n in A]
start = time()
i = np.argsort(A[0])
for n in xrange(len(A)):
A[n] = A[n][i]
print time() - start
start = time()
zipped = zip(*B)
zipped.sort()
B = zip(*zipped)
print time() - start
``` |

The following was printed out

```
0.0469999313354
0.75
```

So it does the same operation 16x faster.

Odly, changing the rows to 100 reduces the speedup to only 10x (printed values of 0.764999866486 7.8900001049). I was expecting the opposite