SORT BY <fields> USING <fields>

Hi All,

I know that USING SORT BY <fields> USING <fields> will cause the fields mentioned to be kept in intemediate storage. Otherwise, they may be emptied by the SORT operation. But I am not sure how this processing takes place. Can anyone explain with one example?

Thanks,
Rohan Joseph.

Here is a fairly simple example:

  • Here we have employed the USING clause to specify which
  • variables we want written to the spinoff file; in our case
  • LEAVE-DUE and SEX. Note that these variables are “correct”
  • in the second loop while NAME and #DOUBLE-LEAVE-DUE
  • are incorrect.
  • This program is functionally identical to the program SORT02
  • However, this program is in structured mode while SORT02
  • is in Mixed mode.

DEFINE DATA LOCAL
1 VIEW1 VIEW OF EMPLOYEES
2 NAME
2 FIRST-NAME
2 LEAVE-DUE
2 SEX
1 #DOUBLE-LEAVE-DUE (N5)
END-DEFINE
*
INCLUDE AATITLER
INCLUDE AASETC
*
*
READ (6) VIEW1 IN LOGICAL SEQUENCE BY NAME STARTING FROM ‘G’
COMPUTE #DOUBLE-LEAVE-DUE = 2 * LEAVE-DUE
DISPLAY *COUNTER NAME FIRST-NAME LEAVE-DUE #DOUBLE-LEAVE-DUE SEX
END-ALL
SORT BY FIRST-NAME USING VIEW1.LEAVE-DUE VIEW1.SEX
AT START OF DATA WRITE / ‘SORTED DATA STARTS HERE’ / END-START
DISPLAY *COUNTER NAME FIRST-NAME LEAVE-DUE #DOUBLE-LEAVE-DUE SEX
END-SORT
END
========================= OUTPUT =========================
PAGE # 1 DATE: 22-Feb-2008
PROGRAM: SORT04 LIBRARY: SNDEMO

CNT             NAME              FIRST-NAME      LEAVE #DOUBLE-LEAVE-DUE S
                                                   DUE                    E
                                                                          X

      1 GARAICOECHEA         USUE                  20       40            F
      2 GARAICOECHEA         LUIS                  26       52            M
      3 GARCIA               M. DE LAS MERCEDES    20       40            F
      4 GARCIA               ENDIKA                30       60            M
      5 GARCIA               GERARDO               25       50            M
      6 GARCIA               PEDRO                 30       60            M

SORTED DATA STARTS HERE

      6 GARCIA               ENDIKA                30       60            M
      6 GARCIA               GERARDO               25       60            M
      6 GARCIA               LUIS                  26       60            M
      6 GARCIA               M. DE LAS MERCEDES    20       60            F
      6 GARCIA               PEDRO                 30       60            M
      6 GARCIA               USUE                  20       60            F

Natural READs a record; processes the record (the COMPUTE and DISPLAY), then generates a spinoff record. The fields in the spinoff record are the sort field(s) , in this case FIRST-NAME, and the fields in the USING clause, in this case LEAVE-DUE and SEX.

Natural continues until the first loop is finished (6 records in this case). Now Natural sorts the spin off records.

Now Natural reads the first spinoff record and processes it (the DISPLAY). Natural continues this for all the 6 spinoff records.

Note that a field that is not mentioned in the USING clause, for example NAME, #LEAVE-DUE, is not “emptied” as you suggested. Instead, it has whatever value it had before the spinoff file was SORTed. When Natural “reads” the spinoff records, these fields are not affected, hence, as just mentioned, they “keep” their last value.

steve