https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108556
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> --- Compiling with -D_GLIBCXX_DEBUG shows the problem: /home/jwakely/gcc/13/include/c++/13.0.1/bits/stl_algo.h:4892: In function: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = gnu_debug::_Safe_iterator<gnu_cxx::normal_iterator<Item*, vector<Item, allocator<Item> > >, debug::vector<Item>, random_access_iterator_tag>; _Compare = main()::<lambda(const Item&, const Item&)>] Error: comparison doesn't meet irreflexive requirements, assert(!(a < a)). Objects involved in the operation: instance "functor" @ 0x7ffee875415f { type = main::{lambda(Item const&, Item const&)#1}; } iterator::value_type "ordered type" { type = Item; } Aborted (core dumped) Your comparison function fails to meet the requirements of a strict weak ordering, so your program has undefined behaviour. https://www.boost.org/sgi/stl/StrictWeakOrdering.html https://en.cppreference.com/w/cpp/named_req/Compare