[Numpy-discussion] Sorting of an array row-by-row?

Kirill Balunov kirillbalunov at gmail.com
Fri Oct 20 06:00:00 EDT 2017


I was trying to sort an array (N, 3) by rows, and firstly come with this

N = 1000000
arr = np.random.randint(-100, 100, size=(N, 3))
dt = np.dtype([('x', int),('y', int),('z', int)])

Then I found another way using lexsort function


*idx = np.lexsort([arr[:, 2], arr[:, 1], arr[:, 0]])*

*arr = arr[idx]*
Which is 4 times faster than the previous solution. And now i have several

Why is the first way so much slower?
What is the fastest way in numpy to sort array by rows?
Why is the order of keys in lexsort function reversed?

The last question  was really the root of the problem for me with the
lexsort function.
And I still can not understand the idea of ​​such an order (the last
is the primary), it seems to me confusing.

Thank you!!! With kind regards, Kirill.

p.s.: One more thing, when i first try to use lexsort. I catch this
strange exception:

*np.lexsort(arr, axis=1)*

                                Traceback (most recent call
last)<ipython-input-278-5162b6ccb8f6> in <module>()----> 1
np.lexsort(ls, axis=1)
AxisError: axis 1 is out of bounds for array of dimension 1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20171020/3ed02a7d/attachment-0001.html>

More information about the NumPy-Discussion mailing list