Sorting 2D array by Mathematical Equation
<p>I have a 2D array list that contains (x,y), however I want to sort this list by the Equation of Minimum value of (square root of (x^2 + y^2)).</p>

<p>For example I have these four 2D lists:</p>

<pre><code>(20,10)
(3,4)
(5,6)
(1.2,7)
</code></pre>

<p>If I take the square root of each 2D array in this list and return the minimum of the sorted list, the output is:</p>

<pre><code>(3,4)
(1.2,7)
(6.5,4)
(5,6)
(20,10)
</code></pre>

<p>the Code :</p>

<pre><code>M=[ [20,10],[3,4],[5,6],[1.2,7],[6.5,4]]
</code></pre>

<p>s=np.sqrt(M)</p>

<p>a=[]</p>

<p>print s</p>

<p>for i in range(0,h):</p>

<pre><code>  for j in range(0,w):

     a[i] =s[i][j]+a[i]
</code></pre>

<p>Any ideas?</p>

<p>the code below will solve what you request, uncomment the print statements if you wish to see how the ordering works!!</p>

<pre><code>import math
array = [(20,10), (3,4), (5,6), (1.2,7)]
sortList = []
count = 0
tempList = []
placeholder = []
#Compute the Equation of Minimum Value
for x,y in array:
    tempList.append(math.sqrt((x**2) + (y**2)))
    tempList.append(array[count])
    sortList.append(tempList)
    tempList = []
    count += 1
#Sort list
count = 1
placeholder  = sortList[0][:]
##print('ORIGINAL LIST\n', sortList)
while count &lt; (len(sortList)):
    if sortList[count - 1][0] &lt; sortList[count][0]:
##        print('THIS IS COUNT', count)
        count += 1
    else:
        placeholder = sortList[count - 1][:]
##        print("this is placeholder: ", placeholder)
        sortList[count - 1] = sortList[count]
##        print(sortList)
        sortList[count] = placeholder
##        print(sortList)
        placeholder = []
        count = 1
</code></pre>

python arrays sorting