complete mainsort and creategroups