Ensure a stable sort based on format