Sort items before grouping