Sorting arrays using alternate properties

Tags:
AS/400 Arrays
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?
ASKED: November 11, 2008  5:49 PM
UPDATED: November 12, 2008  4:02 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following