faster sort + optional group