Fix StatementInvalid error when sorting groups