Added the groupsort logic