QSORT

Perform an in-memory sort

WSupported on Windows
USupported on Unix
VSupported on OpenVMS
NSupported in Synergy .NET
xcall QSORT(array, elements[, function])

Arguments

array

The first element of an array of fields or structures to sort. (a or n)

elements

The number of elements in the array. (n)

function

(traditional Synergy only) (optional) The name of the function for element comparisons. The function is a ^VAL function with two arguments that are each an array element. See the example in the Discussion below. (a)

Discussion

The QSORT subroutine is an in-memory sort subroutine.

QSORT is a very simple comparison routine; the complexity is up to you.

If function is not present, the comparison is alpha for the total length of the array elements.

(traditional Synergy only) If function is present, the named ^VAL function must be accessible when the QSORT subroutine is invoked. The function must have two arguments of the same type that match the type of an array element, and it must have the same format as the following:

function COMPARISON_ROUTINE, ^val
    element_1   ,a              ;Argument type must be the same as the 
    element_2   ,a              ; array element
proc
    if (element_1 .lt. element_2)
      freturn(-1)               ;Return element #1 < element #2
    if (element_1 .gt. element_2)
      freturn(1)                ;Return element #1 > element #2
    freturn(0)
endfunction

Examples

In the following example, an array of 100 records containing client information that consists of an ID, name, and address is sorted by address. In the comparison function, the two arguments are each templates of a single array element.

function COMPARE_CLIENTS, ^val
    group a_g1   ,a             ;Comparison function passed two arguments
      id        ,d6
      name      ,a40
      addr      ,a20
    endgroup
    group a_g2 ,a               ;Both elements of the array to be sorted.
      id        ,d6             ; The comparison function has complete 
      name      ,a40            ; control over deciding exactly how the
      addr      ,a20            ; array elements collate. The comparison
    endgroup                    ; function simply returns the following:
                                ;    ARG1 = ARG2 :  0
                                ;    ARG1 < ARG2 : <0
                                ;    ARG1 > ARG2: >0
proc
    if (a_g1.addr .lt. a_g2.addr)
      freturn(-1)
    if (a_g1.addr .gt. a_g2.addr)
      freturn(1)
    freturn(0)
endfunction