title: Sort a numpy array by another array, along a particular axis
body: <p>Similar to <a href="https://stackoverflow.com/questions/1903462/how-can-i-zip-sort-parallel-numpy-arrays/1903579#1903579">this answer</a>, I have a pair of 3D numpy arrays, <code>a</code> and <code>b</code>, and I want to sort the entries of <code>b</code> by the values of <code>a</code>. Unlike <a href="https://stackoverflow.com/questions/1903462/how-can-i-zip-sort-parallel-numpy-arrays/1903579#1903579">this answer</a>, I want to sort only along one axis of the arrays.</p>
  
  <p>My naive reading of the <code>numpy.argsort()</code> documentation:</p>
  
  <pre><code>Returns
  -------
  index_array : ndarray, int
      Array of indices that sort `a` along the specified axis.
      In other words, ``a[index_array]`` yields a sorted `a`.
  </code></pre>
  
  <p>led me to believe that I could do my sort with the following code:</p>
  
  <pre><code>import numpy
  
  a = numpy.zeros((3, 3, 3))
  a += numpy.array((1, 3, 2)).reshape((3, 1, 1))
  print "a"
  print a
  """
  [[[ 1.  1.  1.]
    [ 1.  1.  1.]
    [ 1.  1.  1.]]
  
   [[ 3.  3.  3.]
    [ 3.  3.  3.]
    [ 3.  3.  3.]]
  
   [[ 2.  2.  2.]
    [ 2.  2.  2.]
    [ 2.  2.  2.]]]
  """
  b = numpy.arange(3*3*3).reshape((3, 3, 3))
  print "b"
  print b
  """
  [[[ 0  1  2]
    [ 3  4  5]
    [ 6  7  8]]
  
   [[ 9 10 11]
    [12 13 14]
    [15 16 17]]
  
   [[18 19 20]
    [21 22 23]
    [24 25 26]]]
  """
  print "a, sorted"
  print numpy.sort(a, axis=0)
  """
  [[[ 1.  1.  1.]
    [ 1.  1.  1.]
    [ 1.  1.  1.]]
  
   [[ 2.  2.  2.]
    [ 2.  2.  2.]
    [ 2.  2.  2.]]
  
   [[ 3.  3.  3.]
    [ 3.  3.  3.]
    [ 3.  3.  3.]]]
  """
  
  ##This isnt' working how I'd like
  sort_indices = numpy.argsort(a, axis=0)
  c = b[sort_indices]
  """
  Desired output:
  
  [[[ 0  1  2]
    [ 3  4  5]
    [ 6  7  8]]
  
   [[18 19 20]
    [21 22 23]
    [24 25 26]]
  
   [[ 9 10 11]
    [12 13 14]
    [15 16 17]]]
  """
  print "Desired shape of b[sort_indices]: (3, 3, 3)."
  print "Actual shape of b[sort_indices]:"
  print c.shape
  """
  (3, 3, 3, 3, 3)
  """
  </code></pre>
  
  <p>What's the right way to do this?</p>