Return to the RPG Tips
Subfile Sort
* DS' for switching sub-file recs during sort routine. The first * data structure must be defined with the exact name, type, and size * of the fields used within the sfile record format. ISFLDS1 IDS I 1 5 FLD1 I 6 10 FLD2 I 11 12 FLD3 ISFLDS2 IDS 12 ISFLDS3 IDS 12 * * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * SRTSFL - Routine to sort sub-file CSR SRTSFL BEGSR * * This routine is based on the "Shell Sort" algorithm. The Shell Sort * algorithm is similar to the bubble sort algorithm. However, shell sort * begins by comparing elements that are far apart (separated by the value * of the offset variable, which is initially half the distance between the * first and last element), then comparing elements that are closer * together (when offset is one, the last iteration of this procedure is * merely a bubble sort). * * * Initialize misc variables C Z-ADD0 SWITCH 20 * * Set comparison offset to half the number of records C ENDRN DIV 2 OFFSET 20 * * DO WHILE Offset > 0 C OFFSET DOWGT0 C ENDRN SUB OFFSET LIMIT 20 * DO UNTIL Switch = 0 * assume no switches at this offset C SWITCH DOUEQ0 C Z-ADD0 SWITCH * compare elements & switch ones out of order C DO LIMIT X 40 C X ADD OFFSET Y 40 C X CHAINSFLREC 80 * C MOVE FLD2 WKA010 10 C MOVELSFLDS1 SFLDS2 * C Y CHAINSFLREC 80 C WKA010 IFGT FLD2 C MOVELSFLDS1 SFLDS3 P C MOVELSFLDS2 SFLDS1 C UPDATSFLREC C X CHAINSFLREC 80 C MOVELSFLDS3 SFLDS1 C UPDATSFLREC C Z-ADDX SWITCH C ENDIF * C ENDDO * * switch on next pass only to where last switch was made C SWITCH SUB OFFSET LIMIT C ENDDO * * No switches at last offset, try on half as big C OFFSET DIV 2 OFFSET C ENDDO * C ENDSR OR FYI, there is also an article in the October 1996 News/400 about using the QLGSORT API to sort subfiles. Here's the URL: http://www.as400network.com/article.cfm?ID=1933 I haven't used this technique so I can't vouch for it's ease of use or performance. - -Paul
[report a broken link by clicking here]