PHP’s sorts are not stable, so two elements the comparator calls equal may come out in either order — and that order can differ between array sizes, because the implementation switches strategy.
// unstable: two orders with the same date swap unpredictably
usort($orders, function ($a, $b) {
return $a->placed_at <=> $b->placed_at;
});
// stable, because no two elements are ever equal
usort($orders, function ($a, $b) {
return [$a->placed_at, $a->id] <=> [$b->placed_at, $b->id];
});
The array comparison with the spaceship operator is the neatest way to express a tiebreaker, and it reads better than a nested conditional. This matters most where a sorted list is rendered and compared between environments — a test that passes locally and fails in CI on a differently sized fixture is usually this. Sorting became stable in 8.0, which is worth knowing about but does not help anyone today.