Sort matrix based on its diagonal entries
<p>First of all I would like to point out that my question is different than this one: <a href="https://stackoverflow.com/questions/12065649/sort-a-numpy-matrix-based-on-its-diagonal">Sort a numpy matrix based on its diagonal</a> </p>

<p>The question is as follow: 
Suppose I have a numpy matrix </p>

<pre><code>A=

5 7 8

7 2 9

8 9 3
</code></pre>

<p>I would like to sort the matrix based on its diagonal and then re-arrange the matrix element based on it. Such that now</p>

<pre><code>sorted_A:

2 9 7

9 3 8

7 8 5
</code></pre>

<p>Note that: </p>

<p>(1). The diagonal is sorted</p>

<p>(2). The other elements (non-diagonal) re-adjusted by it. How?
because diag(A)= [5,2,3] &amp; diag(sorted_A)=[2,3,5] 
so row/column indices A=[0,1,2] become [1,2,0] in sorted_A.</p>

<p>So far I use brute force where I extract the diagonal elements, get the indices O(N²) and then re-arrange the matrix (another O(N²)). I wonder if there is any efficient/elegant way to do this. I appreciate all the help I can get.</p>