I don’t know why, but grouping records in in C/AL in the past had always been a difficult topic to understand for new programmers during my development courses.
In C/AL the best method was using temporary tables, as in the example below, in which I group the Sales Orders by Customer and then I make a loop over the customers I found to make something.