I'm a bit confused about your odd_first comparator: it looks to me like it doesn't implement a strict order (for example odd_first(1, 1) = true, i.e. not irreflexive, and odd_first(2, 4) = odd_first(4, 2) = false, i.e. not antisymmetric). What happens if you give such a "weird" comparator to a sorting function? From your example it looks like it works fine at least in this case...
Nicely spotted, I indeed made a mistake here. My odd_first comparator does not implement a strict order and as such the output of the sort algorithm is undefined. What will actually happen is going to depend on the implementation of the standard library that your are using, but you are indeed not guaranteed that your output will be sorted. Thanks for pointing out the mistake! For those wondering what the requirements are for a comparator when used with STL algorithms: Your comparison function should implement a strict weak ordering: For all x: x < x is never true, everything should be equal to itself If x < y then y < x cannot be true If x < y and y < z then x < z, the ordering should be transitive If x == y and y == z then x == z, equality should be transitive See also: en.cppreference.com/w/cpp/named_req/Compare
I'm a bit confused about your odd_first comparator: it looks to me like it doesn't implement a strict order (for example odd_first(1, 1) = true, i.e. not irreflexive, and odd_first(2, 4) = odd_first(4, 2) = false, i.e. not antisymmetric). What happens if you give such a "weird" comparator to a sorting function? From your example it looks like it works fine at least in this case...
Nicely spotted, I indeed made a mistake here. My odd_first comparator does not implement a strict order and as such the output of the sort algorithm is undefined. What will actually happen is going to depend on the implementation of the standard library that your are using, but you are indeed not guaranteed that your output will be sorted. Thanks for pointing out the mistake!
For those wondering what the requirements are for a comparator when used with STL algorithms:
Your comparison function should implement a strict weak ordering:
For all x: x < x is never true, everything should be equal to itself
If x < y then y < x cannot be true
If x < y and y < z then x < z, the ordering should be transitive
If x == y and y == z then x == z, equality should be transitive
See also: en.cppreference.com/w/cpp/named_req/Compare