Jason's solution works if a
has no repeating values. If a
has values that repeat, the method is not equivalent to the zip(*sorted(zip(a,b)))
method (it only sorts on a
, whereas the zip
method sorts on both a
and b
):
>>> a = numpy.array([1, 2, 3, 1, 1, 1])
>>> b = numpy.array([9, 4, 6, 2, 5, 7])
>>> zip(*sorted(zip(a,b)))
[(1, 1, 1, 1, 2, 3), (2, 5, 7, 9, 4, 6)]
>>> b[a.argsort()]
array([9, 2, 5, 7, 4, 6]) # could be different for different python/numpy versions.
If that is okay with you, then that seems to be the easiest method. Otherwise, use Peter's solution:
>>> c = numpy.rec.fromarrays([a, b], names='a,b')
>>> c.sort()
>>> c.a
array([1, 1, 1, 1, 2, 3])
>>> c.b
array([2, 5, 7, 9, 4, 6])