Andrew Barnert wrote: > On Dec 26, 2019, at 12:19, Marco Sulla via Python-ideas > [email protected] wrote: > you can get the behavior of your algorithm below: > @functools.cmp_to_key > def flip_incomparables_key(a, b): > if a < b: return -1 > if b < a: return 1 > return 1
^_____^ we arrived to the same conclusion: https://mail.python.org/archives/list/[email protected]/message/LM22TNILN2INCZVIPXIUEOKHZ5RJKS5Q/ But you're key is better! It's not only for NaNs, and I don't know why you say it does not do exactly what I wanted. > > If also this check returns 0, there are > > two cases: > > > > the objects are equal > > elem_a (or also elem_b) are not orderable > > > > In this case, there's no problem: the function will switch elem_a and > > elem_b. If they are equal, it changes nothing. > No, it breaks the stability property. Two values can compare equal but be > meaningful distinct. This is especially important when using a key > function—e.g., the > common “sort by column C, then by column B” idiom relies on the fact that two > values that > are equal in column B but different in column C remain in their column-C > order. Well, so we can't use __lt__(). We have to require __le__(): 1. if a <= b is 1, do nothing 2. if a <=b is 0, then switch This way, equal objects are not switched. Greater and uncomparable objects are switched. Call it IliadSort :D (Well, I think this joke is for Italians only....) > What should happen if a and b are incomparable, but > a>c while b<c? bca > What if b and c are also incomparable? bca or cba > So you can’t just say “push all the incomparable values to the right”. Wait, I'll push them until they are incomparable. I always check for "<=" for every element, I don't mark them as "incomparable" the first time and stop. I think we can simply apply to sorted you hack key, with a little mod: ``` @functools.cmp_to_key def iliadSort(a, b): if a <= b: return -1 if b <= a: return 0 return 1 ``` I'll test it. _______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/PLXHE4U5AR4YJARLK7NDBPH6KELPJFBJ/ Code of Conduct: http://python.org/psf/codeofconduct/
