# SORT BY \<fields\> USING \<fields\>

**URL:** <https://techcommunity.softwareag.com/t/sort-by-fields-using-fields/76209>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [February 22, 2008, 2:58pm UTC](https://techcommunity.softwareag.com/t/sort-by-fields-using-fields/76209 "2008-02-22T14:58:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohan](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@Rohan](https://techcommunity.softwareag.com/u/Rohan)\
**Post date:** [February 22, 2008, 2:58pm UTC](https://techcommunity.softwareag.com/t/sort-by-fields-using-fields/76209/1 "2008-02-22T14:58:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [February 22, 2008, 6:16pm UTC](https://techcommunity.softwareag.com/t/sort-by-fields-using-fields/76209/2 "2008-02-22T18:16:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:49pm UTC](https://techcommunity.softwareag.com/t/sort-by-fields-using-fields/76209/3 "2021-04-02T14:49:10Z")

</div>


