Using RPGIV, you have the opportunity to do something which at first sight looks rather strange:
D Records Dim(150)
D UNPR 4 Overlay( Records : 1 )
D UNNO 8 Overlay( Records : *Next )
D RCID 9S 0 Overlay( Records : *Next )
D Type 1 Overlay( Records : *Next )
In the example, RECORDS is (obviously) an array with 150 elements. The length of an element is defined by the following fields in the structure. Each field follows the next.
So, a single element of RECORD contains UNPR, then UNNO, then RCID, then TYPE.
But - and here is the clever part - UNPR is defined in the program as an array itself. And so is UNNO, and so are all the other subfields.
So you can reference an element of the array UNPR - for example, UNPR (12).
Now - and this really IS the clever part - you can SORTA on ANY of those arrays, and the other arrays will be re-arranged automatically.
In your example, SORTA ACC will sort your data into account order (and the SUM array will be re-arranged so that its values will be correctly associated with the account values), and then you can SORTA SUM to re-sort the data into the sum order, with the accounts being re-arranged automatically.
Once the 'real' array (RECORD in the example) has been defined, you should not need to refer to it again - just refer to the subfield arrays. But if you want to, then clearing RECORDS will clear all the subfield arrays, and referencing RECORD ( n ) will return a field which contains the concatenated values in the corresponding elements of the subfield arrays.