title: Sorting numpy array in the same manner as sorted(, key=)
body: <p>For example, to sort an array of complex numbers first by real part, then by magnitude of imaginary part, and then with negative imaginary parts first:</p>
  
  <pre><code>def order(a):
      return a.real, abs(a.imag), sign(a.imag)
  
  z = array(sorted(z, key=order))
  </code></pre>
  
  <p>So </p>
  
  <p><code>array([ 1.+2.j, 5.+0.j, 1.+0.j, 1.+1.j, 1.+1.j, 1.-1.j, 6.+0.j, 1.-1.j, 1.-2.j])</code></p>
  
  <p>becomes </p>
  
  <p><code>array([ 1.+0.j, 1.-1.j, 1.-1.j, 1.+1.j, 1.+1.j, 1.-2.j, 1.+2.j, 5.+0.j, 6.+0.j])</code></p>
  
  <p>I think there's a way to do the same thing using numpy's <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html" rel="noreferrer">argsort</a>, which is probably faster, but I can't figure it out:</p>
  
  <pre><code>In [2]: argsort((a.real, abs(a.imag), sign(a.imag)))
  Out[2]: 
  array([[0, 2, 3, 4, 5, 7, 8, 1, 6],
         [1, 2, 6, 3, 4, 5, 7, 0, 8],
         [5, 7, 8, 1, 2, 6, 0, 3, 4]])
  </code></pre>