title: Reverse sort and argsort in python
body: <p>I'm trying to write a function in Python (still a noob!) which returns indices and scores of documents ordered by the inner products of their tfidf scores. The procedure is:</p>
  
  <ul>
  <li>Compute vector of inner products between doc <code>idx</code> and all other documents</li>
  <li>Sort in descending order</li>
  <li>Return the "scores" and indices from the second one to the end (i.e. not itself)</li>
  </ul>
  
  <p>The code I have at the moment is:</p>
  
  <pre><code>import h5py
  import numpy as np
  
  def get_related(tfidf, idx) :
      ''' return the top documents '''
  
      # calculate inner product   
      v = np.inner(tfidf, tfidf[idx].transpose())
  
      # sort
      vs = np.sort(v.toarray(), axis=0)[::-1]
      scores = vs[1:,]
  
      # sort indices
      vi = np.argsort(v.toarray(), axis=0)[::-1]
      idxs = vi[1:,] 
  
      return (scores, idxs)
  </code></pre>
  
  <p>where <code>tfidf</code> is a <code>sparse matrix of type '&lt;type 'numpy.float64'&gt;'</code>.</p>
  
  <p>This seems inefficient, as the sort is performed twice (<code>sort()</code> then <code>argsort()</code>), and the results have to then be reversed. </p>
  
  <ul>
  <li>Can this be done more efficiently? </li>
  <li>Can this be done without converting the sparse matrix using <code>toarray()</code>?</li>
  </ul>