Skip to content
Advertisement

Tag: sorting

Python: sort function breaks in the presence of nan

sorted([2, float(‘nan’), 1]) returns [2, nan, 1] (At least on Activestate Python 3.1 implementation.) I understand nan is a weird object, so I wouldn’t be surprised if it shows up in random places in the sort result. But it also messes up the sort for the non-nan numbers in the container, which is really unexpected. I asked a related question

How to sort a list of strings numerically?

I know that this sounds trivial but I did not realize that the sort() function of Python was weird. I have a list of “numbers” that are actually in string form, so I first convert them to ints, then attempt a sort. Gives me: What I want is I’ve looked around for some of the algorithms associated with sorting numeric

How to sort objects by multiple keys?

Or, practically, how can I sort a list of dictionaries by multiple keys? I have a list of dicts: and I need to use a multi key sort reversed by Total_Points, then not reversed by TOT_PTS_Misc. This can be done at the command prompt like so: But I have to run this through a function, where I pass in the

Advertisement