## Question:

I have`n x 3`

ndarray and wanted to sort it by 3-rd column.In 3-rd column there MAY BE some `np.inf`

. Anyway, I used `sorted`

function. After that I used `njit`

decorator on my main function but got different result… So I wrote that code to check what’s going on:`samples = [0, 1, 2, 442]`

– there is no differences. Why?## Answer:

Your function tell to Numba/CPython to sort lines based on the second item which contains Inf values. Two infinity values are considered equal so a sorting algorithm can change the order of the lines having Inf values in the second item of the target lines. The different results is due to different algorithms being used. This is expected because the**sorting algorithm is not guaranteed to be stable**(ie. preserve the order of

*equal lines*).

To solve the problem, you need to use a stable algorithm.

**can be used to find the ordering of the value and the parameter**

`np.argsort`

`kind`

can be tuned so to choose a stable algorithm (called `"stable"`

). Additionally, `np.argsort`

should also be faster than `sorted`

on Numpy arrays because it does not cause many lambda calls nor compute Numpy lines as slow CPython objects. This should do the job:If you have better answer, please add a comment about this, thank you!