This page describes how to sort an array in Rexx. There is, 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.

The SORTDEMO Exec

Building the Array

The first action that the SORTDEMO exec does is build an array in order to demonstrate the sort process.
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:
  • The first field (in this case Linkmod)
  • The first field and then the second field
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.

Fill and List the Array

Call BuildArray

lx = 8      /* Number of elements in array */

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.

Sort the Array by First Field

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).

Verify the Sort

say '--- Sorted Table ---'
Do x = 1 to lx
   say Linkmod.x 'is used via a' LinkHow.x
End

Call BuildArray
Now we relist to 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 wher ewe sort on both fields

Sort the Array by 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.

Output Produced by the SORTDEMO exec

Output from the SORTDEMO exec 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 the top menu under "z/OS Based Software Inventory" or from here.

It is also available directly from here.
 
 


If you need any support or assistance with any of the code on this site
or
if you would like to contact us, please click here

follow us on facebook
Follow us on Facebook

 

Number of unique visitors 300

Copyright © Abbydale Systems LLC 2015-2024

Abbydale Systems LLC Lic. 802696149. All rights reserved.

Last modified : Saturday 15th of July 2023