Sorting arrays using alternate properties
I have table1 with 2 columns: account and sum. acc sum($) c08 100 c02 200 c01 256 c08 300 I want to put in table2, the same columns, but in order: acc sum($) c01 256 c02 200 c08 400 I use two alternate arrays for account and for sum. How can I sort sum array using alternate property and account array?

Software/Hardware used:
ASKED: November 11, 2008  5:49 PM
UPDATED: November 12, 2008  4:02 PM

Answer Wiki:
Using RPGIV, you have the opportunity to do something which at first sight looks rather strange: <pre> D DS 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 ) </pre> 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. Regards, Sloopy
Last Wiki Answer Submitted:  November 12, 2008  4:02 pm  by  Sloopy   2,195 pts.
All Answer Wiki Contributors:  Sloopy   2,195 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _