Rexx Facility : RexxSortDemo

Navigate back to home page
z/OS related collateral
Windows related collateral
SQL/mySQL related collateral
Link to code that can be used on websites.
Link to ASL Program Documentation
All our latest news
Contacting Abbydale Systems
Obtain product support/suggestions
Terms and conditions for the site
General disclaimer
All about Abbydale Systems
How we got our company name
Our privacy policy
Our development strategy
Visit our guestbook

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.

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

/* 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.


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


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


This material has been used successfully by Abbydale Systems LLC. and to the best of our knowledge this material and any system(s) of which it is a part are operational as of the service level or date stated in the body of this material (if so stated). However, NO warranty is given or implied as to the accuracy of this material or any related material or systems, and no responsibility is assumed for any effect or modification directly or indirectly caused by the use of this material.

It is the responsibility of any user of this material to evaluate its usefulness to the user's environment.

Abbydale Systems LLC. does not guarantee to keep this or any related material current, nor does it guarantee to provide any corrections or extensions described by any users of this material or any corrections or extensions made in the future by Abbydale Systems LLC. itself.


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

follow us on facebook
Follow us on Facebook




Subscribe to our YouTube Channel Subscribe to our YouTube channel


CBT Tape Home Page MVS and OS/390 Freeware Link to the CBT website


Copyright © Abbydale Systems LLC 2015-2026

Abbydale Systems LLC Lic. 802696149. All rights reserved.