# Is there an internal table sort command in natural?

**URL:** <https://techcommunity.softwareag.com/t/is-there-an-internal-table-sort-command-in-natural/90641>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [July 12, 2009, 5:17pm UTC](https://techcommunity.softwareag.com/t/is-there-an-internal-table-sort-command-in-natural/90641 "2009-07-12T17:17:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anik](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@anik](https://techcommunity.softwareag.com/u/anik)\
**Post date:** [July 12, 2009, 5:17pm UTC](https://techcommunity.softwareag.com/t/is-there-an-internal-table-sort-command-in-natural/90641/1 "2009-07-12T17:17:27Z")

</div>

Is there an internal table sort command in natural?

Thanks  
Alex

---

<div class="post-metadata">

**Author:** ![Douglas\_Kelly](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/douglas_kelly/32/102_2.png) [@Douglas\_Kelly](https://techcommunity.softwareag.com/u/Douglas_Kelly)\
**Post date:** [July 13, 2009, 4:23am UTC](https://techcommunity.softwareag.com/t/is-there-an-internal-table-sort-command-in-natural/90641/2 "2009-07-13T04:23:21Z")

</div>

there isn’t a separate command for sorting tables from any other sort. You can use the regular sort command to sort the contents of a table. Somewhat fiddly.

In structured mode, you can use a FOR loop to iterate over the table, include each of the array columns in the SORT USING clause, including the FOR index. Post-sort, repopulate the table from the returned values.

If you are going to do this in batch, put the SORT into a subprogram (pass in the unsorted table, return the sorted table) and, if the table sort is going to be done repeatedly, you may want to investigate the dynamic parameters for modifying the external sort parameters (set it to not release sort work areas, for example) or simply invoke the internal (memory) sort.

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [July 13, 2009, 4:55am UTC](https://techcommunity.softwareag.com/t/is-there-an-internal-table-sort-command-in-natural/90641/3 "2009-07-13T04:55:27Z")

</div>

Here’s an example from our Intermediate Natural class:

First the mainline:

```auto
DEFINE DATA /* M3SORTP
LOCAL
1 #MAX (I4) CONST <10>
1 #KEY (A15/#MAX) INIT <'Smith', 'Jones', 'Able',
                              'Charlie', ' ', 'Baker'>
1 #OTHER (A25/#MAX) INIT <'Jane', 'Andrew', 'Michael',
                              'Alice', ' ', 'Margaret'>
1 #TOP (I4) INIT <6>
END-DEFINE
*
DISPLAY
        #MAX
        #TOP
        #KEY (1:#TOP)
        #OTHER (1:#TOP)
CALLNAT "M3SORTN" #KEY (1:#TOP)
                  #OTHER (1:#TOP)
SKIP 1
DISPLAY
        #MAX
        #TOP
        #KEY (1:#TOP)
        #OTHER (1:#TOP)
END
```

Then the subprogram:

```auto
DEFINE DATA /* M3SORTN
PARAMETER
1 #KEY (A15/1:V)
1 #OTH (A25/1:V)
LOCAL
1 #SORT (A15)
1 #OTHER (A25)
1 #I (I4)
1 #M (I4)
END-DEFINE
*
ASSIGN #M = *OCC (#KEY)
FOR #I = 1 #M
  ASSIGN #SORT = #KEY (#I)
  ASSIGN #OTHER = #OTH (#I)
END-ALL
*
SORT BY #SORT
  USING #OTHER
  AT START OF DATA
    RESET #I
          #KEY (*)
          #OTH (*)
  END-START
  /*
  IF #SORT <> ' '
   AND #OTHER <> ' '
    THEN
      ADD 1 TO #I
      ASSIGN #KEY (#I) = #SORT
      ASSIGN #OTH (#I) = #OTHER
  END-IF
END-SORT
END
```

---

<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:57pm UTC](https://techcommunity.softwareag.com/t/is-there-an-internal-table-sort-command-in-natural/90641/4 "2021-04-02T14:57:15Z")

</div>


