On Fri, Dec 27, 2019 at 9:07 AM Andrew Barnert via Python-ideas <python-ideas@python.org> wrote: > > You can very easily just write a key function that does this for you. In > fact, you can write different key functions for different variations. > > For example, if you’re specifically sorting floats and want to shift nans to > the right (even beyond inf): > > class shift_nan_to_end_key(float): > def __lt__(self, other): > return math.isnan(other) or not math.isnan(self) and > float(self)<float(other) > > If you want to sort nans to the start, or deal with some other type with > incomparable values, or do something more general with all pairs of > incomparable values, you can just write a different key function. Whatever’s > most appropriate for readability or type safety or performance for your > particular use case, do that. >
You've created a key class, effectively equivalent to a cmp_to_key form. Is there a flaw in this much simpler version? def nans_right(n): return math.isnan(n), n ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/J7DAK2XPWA2UXKL7DFCGVLIQPMWP7663/ Code of Conduct: http://python.org/psf/codeofconduct/