Novel use of sorting

When writing an MQSCX script you can create arrays of data and then sort them. This is aptly demonstrated by a new example script, mqauthlist.mqsx, available in our Example Scripts bundle. This script generates a report, which is sorted into a very specific order before being written out to a file. The way this sorting is done is very novel, and so we thought it worth writing about for you to see.

SortingThis sorting of the results is done so that within each Entity (and each Entity Type), the Object Types are listed in a specific order. This is done by generating a field whose sole purpose is to be sorted.

This field starts with the character 0 or 1 to represent a Group or a Principal, resulting in all the groups being sorted together and then all the Principals being sorted together. Then it has the 12 byte padded entity name (be that a Group or a Principal). Here’s the first part of how the field was constructed.

** Create a special field that will be used to sort the authorization
** records into the order that we want them to appear in the report.
** Generate the SortField as a combination of EntityType, Entity, Objtype, Profile
if( @a_enttype = "GROUP" )
  @sortfld = str(0)
  @sortfld = str(1)
** pad the entity to 12 bytes
@sortfld = @sortfld + @a_entity + substr("                ", 1, 16 - strlen(@a_entity))

Following the entity name is a two-character value for each Object Type, with those two-characters assigned based on the order the report author wanted the objects to be listed in the report – within each user. Finally it has the profile name, so that within each Object Type, the objects are sorted too. Here’s the remainder of how the field was constructed.

** assign a numeric value to the object type so they sort
** in the order we want them to appear in the report
if(@a_objtype = "QUEUE") @sortfld = @sortfld + str(12); endif
if(@a_objtype = "TOPIC") @sortfld = @sortfld + str(13); endif
if(@a_objtype = "PROCESS") @sortfld = @sortfld + str(14); endif

** add the profile
@sortfld = @sortfld + @a_profile

** Add the authorization record into the array
@i = @i + 1
@auths[1, @i] = @sortfld

All the above code takes place in a for loop that goes through each authorization record returned by the DISPLAY AUTHREC. Once that loop is complete, then the array that was created is sorted very simply, using the sort function with the second parameter indicating with column to use to sort the array:-

** Sort the array by sortfld
sort(@auths, 1 )

As you can see, not only is MQSCX able to allow you to write reports to process the interrogations you make of the Queue Manager via the Command Server, the MQSCX Control language is also extremely flexible.

The best way to understand scripts is of course to have a go with them yourself. There are various examples in the download, so why not try them out yourself. If you are not currently an MQSCX licence holder, you may email to request a trial licence.


The team at MQGem would love to hear what you think. Leave your comments here.

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s