Stable sort optimization