sorting when there are pairs