Fix errors when sorting contacts