Is there an internal table sort command in natural?
Thanks
Alex
Is there an internal table sort command in natural?
Thanks
Alex
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.
Here’s an example from our Intermediate Natural class:
First the mainline:
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:
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