Updating the stable sort function