Function Fixing

Fixed errors in sortGroup func and consequential errors due to NaN being returned.