Skip to content
Advertisement

Sorting lists with multiple tie breakers

I have data in an array like so:

JavaScript

I want to sort it by the amount of non-10 values, and I also want to sort it in ascending order for rows, and in descending order of number of 10s:

JavaScript

Output:

JavaScript

I want to implement a tie breaker system so that if the amount of 10s the same, it now orders by amount of 9s, then 8s, and so on. Expected output:

JavaScript

Advertisement

Answer

You can achieve it with numpy.frompyfunc.

The basic idea is to construct an array with the same rows, each element of which is a tuple containing the number of 10s, 9s, etc. Then apply numpy.argsort to this array and get the result.

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