I have a binary function which returns a Boolean indicating whether the first argument is strictly less than the second argument.
Can I use the sorted(my_list, key=functools.cmp_to_key(boolean_cmp)) or some variant thereof to sort this list? Or do I need to refactor the boolean_cmp function so that it returns a positive, negative, or zero integer?
Do I really need to distinguish between the greater-than case and the equal-to case?
The boolean_cmp function is written as a several methods spread out over the program. Refactoring them would be possible, but seems like more work than is necessary.
I've done a bit of experimentation and it looks like converting False to 0 and True to -1 causes sorted to sort correctly. But my experiment is only anecdotal.
BTW, I was reading Sorting HOW TO and it took me a long time to figure out that the underlying compare function needed an integer return value rather than a Boolean. In my opinion, it would have been nice if that had been clearer. [grumble]
lambda a, b : 1 if boolean_cmp(a, b) else -1 if boolean_cmp(b, a) else 0?lambda a, b : 1 if boolean_cmp(a, b) else -1might be sufficient