As ever, you use this free program at your own risk.
Feel free to use the free software on this site but please honor the copyright.
This page describes how to sort an array in Rexx. There are, of course,
many different ways to sort arrays available, but this method is usually
good enough (and fast enough) for small arrays.
For large arrays we recommend using your site standard sort program.
The method described here needs no external sort
program and no additional files are required.
We have released the SORTDEMO Rexx exec (described here) as a
demonstration of how to sort an array.
The fully functioning SORTDEMO Rexx exec is available from
here.
|
BuildArray: Linkmod.1 = 'IEBCOPY' LinkHow.1 = 'Call' Linkmod.2 = 'ASLCOPY' LinkHow.2 = 'Link' Linkmod.3 = 'WHATNAME' LinkHow.3 = 'Link' Linkmod.4 = 'WHATNAME' LinkHow.4 = 'Call' Linkmod.5 = 'IEBUPDTE' LinkHow.5 = 'Call' Linkmod.6 = 'ASLDEL' LinkHow.6 = 'Call' Linkmod.7 = 'ASLQWTO' LinkHow.7 = 'Attach' Linkmod.8 = 'MQERROR' LinkHow.8 = 'Load' return |
We isolated the building of the demonstration array into a separate
procedure name BuildArray.
This was done so that we can quickly and easily rebuild the array in order to demonstrate the sorting of the array by:
Within our demonstration it is called twice. Firstly it is called to build the original array. The second time is called to rebuild it after it has been sorted for the first time. |
|
Call BuildArray /* Number of elements in array */ lx = 8 say '-- Unsorted Table --' Do x = 1 to lx say Linkmod.x 'is used via a' LinkHow.x End |
The first step is to fill the array by calling BuildArray
The next step is to display the array. This is just to prove that the array has been successfully built and is sorted. |
|
Do
x
= 1 to lx
-1 Do r = k + 1 to lx k_Linkmod = linkmod.k k_LinkHow = linkHow.k r_Linkmod = linkmod.r r_LinkHow = linkHow.r If r_Linkmod < k_Linkmod Then Do Linkmod.k = r_Linkmod Linkhow.k = r_Linkmod Linkmod.r = k_Linkmod Linkhow.r = k_Linkmod End End End |
Now we 'bubble' through the array swapping adjacent items (if needed)
into alphabetic order.
In our example only the first item (i.e. Linkmod) will be sorted alphabetically. The corresponding LinkHow field will be swapped with the LinkMod (if needed). |
|
say
'--- Sorted Table ---' Do x = 1 to lx say Linkmod.x 'is used via a' LinkHow.x End Call BuildArray |
Now we relist the array to show that the Linkmod fields are now in
alphabetic order.
Notice that the LinkHow for WHATNAME is still not alphabetic. This is because we were only sorting on the one field. We also call BuildArray after the listing in order to rebuild the original array ready for the second sort where we sort on both fields. |
|
Do
x
= 1 to lx
-1 Do r = k + 1 to lx k_Linkmod = linkmod.k k_LinkHow = linkHow.k r_Linkmod = linkmod.r r_LinkHow = linkHow.r If r_Linkmod < k_Linkmod Then Do Linkmod.k = r_Linkmod Linkhow.k = r_Linkmod Linkmod.r = k_Linkmod Linkhow.r = k_Linkmod End If r_Linkmod = k_Linkmod Then Do If r_LinkHow < k_LinkHow Then Do Linkmod.k = r_Linkmod Linkhow.k = r_Linkmod Linkmod.r = k_Linkmod Linkhow.r = k_Linkmod End End End End |
Now we 'bubble' through the array swapping adjacent items (if needed) into alphabetic order. We will, if the LinkMod names match, sort the LinkHow field into alphabetic order. Obviously the larger the array the longer this process takes, but for the most part it is pretty quick. You can add fields to the array but remember to include these in the 'flip-flop' otherwise your array may become corrupted. The final step would be to list the array again and verify that the sort worked. |
|
Alongside is a screenshot of the execution of the SORTDEMO Rexx exec.
Note in particular the WHATNAME entries in the sorted lists. In the first list the LinkHow values are 'List' then 'Call' whereas in the second sorted list the LinkHow values are now alphabetic. |
SORTDEMO is available from here.
Abbydale Systems LLC Lic. 802696149. All rights reserved.