Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Sort Array

Is it possible to sort an array with the smallest values on the bottom left and the highest on the top right?

For example :

I’ve this array :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[-1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [-1.  1.  1.]]

the sort would be like

[[1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [-1.  1.  1.]
 [-1.  1.  1.]]

An other example :

[[-1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 4.  3.  1.]
 [ 1.  1.  1.]
 [-1.  1.  1.]]

Would be :

[[ 1.  1.  4.]
 [ 1.  1.  3.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]
 [-1.  1.  1.]
 [-1.  1.  1.]]

I Tried Numpy sort :

MyArray.sort()

But it seems not ordering this way.

>Solution :

import numpy as np

x = np.random.rand(6,3)

print(x)

def eiffel_tower_sort(a):
    b = a.flatten()
    b.sort()
    return np.flipud(b.reshape(a.shape, order='F'))

print(eiffel_tower_sort(x))
# randomized / unsorted
[[0.45884748 0.36774746 0.82728461]
 [0.46473908 0.22377053 0.43772489]
 [0.3596408  0.89647436 0.43567059]
 [0.10431368 0.06733271 0.26813345]
 [0.45886791 0.57112807 0.51780818]
 [0.27032551 0.77706324 0.32331996]]

# sorted
[[0.32331996 0.45886791 0.89647436]
 [0.27032551 0.45884748 0.82728461]
 [0.26813345 0.43772489 0.77706324]
 [0.22377053 0.43567059 0.57112807]
 [0.10431368 0.36774746 0.51780818]
 [0.06733271 0.3596408  0.46473908]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading