Guardian Programmer's Guide

Table Of Contents
Formatting and Manipulating Character Data
Guardian Programmer’s Guide 421922-014
19 - 46
Sorting Characters
!------------------------------------------------------------
! Main procedure initializes the IN file and then calls the
! command interpreter.
!------------------------------------------------------------
PROC INITIALIZE MAIN;
BEGIN
! Initialize the IN file:
CALL INITIALIZE^TERMINAL;
! Call the command interpreter:
CALL COMMAND^INTERPRETER;
END;
Sorting Characters
Use the HEAPSORT[X_] procedure to sort an array in memory. You can use the
HEAPSORT procedure only to sort arrays in the user data segment; you cannot use it
to sort arrays in extended memory. You can use HEAPSORTX_ to sort arrays that are
either in the user data segment or in an extended data segment.
To use the HEAPSORT[X_] procedure, you must supply it with the array you want to
sort, the number of elements in the array, the size of each element, and the name of
the user-supplied procedure that will do the actual comparison. HEAPSORTX_ also
has an optional parameter that allows you to specify an array of pointers. This array of
pointers can help speed up the sort by allowing HEAPSORTX_ to sort a list of pointers
instead of the data elements themselves; the pointer array is particularly useful if the
sort involves a large number of elements or a large element size.
CALL HEAPSORTX_(ARRAY,
NUMBER^OF^ELEMENTS,
ELEMENT^SIZE,
ASCENDING, !Name of procedure to do
! comparison
POINTER^ARRAY);
The following sample program sorts some strings into alphabetical order. The program
is made up of three procedures:
The main procedure provides initialization and calls the SORTING procedure.
The SORTING procedure supplies a list of strings to the HEAPSORTX_ procedure
for sorting. On return from HEAPSORTX_, the SORTING procedure displays the
sorted list on the home terminal.
The ASCENDING procedure is called by HEAPSORTX_ to compare pairs of
strings. This procedure returns 1 if the first string is less than the second string or
0 if the second string is less than the first string. HEAPSORTX_ calls this
procedure as many times as it needs to sort the entire list of strings.