Guardian Programmer's Guide

Table Of Contents
Formatting and Manipulating Character Data
Guardian Programmer’s Guide 421922-014
19 - 48
Sorting Characters
!------------------------------------------------------------
! Procedure to initialize an array with string values and
! then call HEAPSORTX_ to sort them. By calling ASCENDING,
! it sorts them into ascending order.
!------------------------------------------------------------
PROC SORTING;
BEGIN
INT I; !counting variable
INT(32) NUMBER^OF^ELEMENTS; !size of array to be
! sorted
STRUCT ARRAY^REF(*); !structure defining an
BEGIN ! array element
STRING ELEMENT[0:11];
END;
STRUCT .ARRAY(ARRAY^REF)[0:9]; !array with 10 elements
! Initialize array for sorting. For simplicity the array
! is initialized statically. In practice, the array would
! typically be read from another file or entered
! interactively at the terminal:
ARRAY[0] ':=' "BUSH ";
ARRAY[1] ':=' "REAGAN ";
ARRAY[2] ':=' "CARTER ";
ARRAY[3] ':=' "FORD ";
ARRAY[4] ':=' "NIXON ";
ARRAY[5] ':=' "JOHNSON ";
ARRAY[6] ':=' "KENNEDY ";
ARRAY[7] ':=' "EISENHOWER ";
ARRAY[8] ':=' "TRUEMAN ";
ARRAY[9] ':=' "WASHINGTON ";
! Sort the array:
NUMBER^OF^ELEMENTS := 10D;
ERROR := HEAPSORTX_(ARRAY,
NUMBER^OF^ELEMENTS,ELEMENT^SIZE,
ASCENDING);
! Print the array in sorted order:
I := 0;
WHILE $DBL(I) < NUMBER^OF^ELEMENTS DO
BEGIN
CALL WRITEX(TERM^NUM,ARRAY[I],(ELEMENT^SIZE * 2));
I := I + 1;
END;
END;