Optimize for speed
Sort before grouping