Skip to content
Advertisement

How can I sort a python list by key without .sort()?

I want to convert this piece of code in order to make it compatible with Numba. The only sort method that Numba support is sorted() but not with the key arg. I have to manualy sort without other lib imports or maybe just some numpy. Someone could give me an efficient way to do this sort ? Thanks

JavaScript

Edit :

JavaScript

Advertisement

Answer

For this particular kind of input, you can achieve the sorting with:

JavaScript

If the keys are not a permutation from a range(n) (like in your question), then create temporary tuples, call sorted and then extract the value again from the tuples:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement