Implement sorting and grouping