Sorting a multidimensional dimensional array of x,y coordinates in python
<p>So I am attempting to sort a multidimensional array of elements in python.  The array is a mess which is what makes it so hard, but unfortunately it is what the opencv function returns.</p>

<p>These are y,x coordinates (backwards I know....)</p>

<pre><code>[array([[[ 13, 178]],[[ 50, 179]],[[ 49, 258]],[[ 12, 257]]]), 
array([[[ 58,  93]],[[105,  94]],[[104, 171]],[[ 57, 170]]]), 
array([[[ 13,  93]],[[ 50,  94]],[[ 49, 171]],[[ 12, 170]]]), 
array([[[ 58,   9]],[[105,  10]],[[104,  86]],[[ 57,  85]]]), 
array([[[13,  9]], [[50, 10]],[[49, 86]],[[12, 85]]]), 
array([[[ 58, 178]],[[105, 179]],[[104, 257]],[[ 57, 257]]])]
</code></pre>

<p>Now the way I need this sorted is by the first element of each numpy array (lets call it a), and then the SECOND element of a in DESCENDING.  And I need all ties to be broken on the FIRST element of a in ASCENDING order.</p>

<p>So this would end up being</p>

<pre><code>[array([[[ 13, 178]],[[ 50, 179]],[[ 49, 258]],[[ 12, 257]]]),
array([[[ 58, 178]],[[105, 179]],[[104, 257]],[[ 57, 257]]]),
array([[[ 13,  93]],[[ 50,  94]],[[ 49, 171]],[[ 12, 170]]]),
array([[[ 58,  93]],[[105,  94]],[[104, 171]],[[ 57, 170]]]),
array([[[13,  9]], [[50, 10]],[[49, 86]],[[12, 85]]]),
array([[[ 58,   9]],[[105,  10]],[[104,  86]],[[ 57,  85]]])]
</code></pre>

<p>Now, I have figured out how to access these elements using</p>

<pre><code>items[each][first][0][Y_POS(or X_POS)]
</code></pre>

<p>where each is the nparray to access (there are 6 of them)
and Y_POS refers to the y position and X_POS refers to the x position.</p>

<p>The other two variables first and 0 will never change.</p>

<p>I really have no idea where to start with this to make it run efficiently, so any help is appreciated.  Keep in mind that the coordinates are in Y,X notation.</p>

<p>Thanks in advance everyone.</p>