sort groups for matching