Initializing help system before first use

# qsort

Purpose
Sort a list or an array or (a subset of) the indices of an array.
Synopsis
`procedure qsort(sense:boolean, lvals:list)`
`procedure qsort(sense:boolean, vals:array of integer|real|string)`
`procedure qsort(sense:boolean, cvals:array|list of array, ndx:array)`
`procedure qsort(sense:boolean, cvals:array|list of array, ndx:array, sel:set)`
`procedure qsort(sense:boolean, cvals:array|list of array, lndx:list)`
`procedure qsort(sense:boolean, cvals:array|list of array, lndx:list, sel:set)`
`procedure qsort(sense:boolean, cmpfct:string|function, cmpctx:?, vals:array, ndx:array)`
`procedure qsort(sense:boolean, cmpfct:string|function, cmpctx:?, vals:array, ndx:array, sel:set)`
`procedure qsort(sense:boolean, cmpfct:string|function, cmpctx:?, vals:array, lndx:list)`
`procedure qsort(sense:boolean, cmpfct:string|function, cmpctx:?, vals:array, lndx:list, sel:set)`
`procedure qsort(sense:boolean, cmpfct:string|function, cmpctx:?, lvals:list)`
Arguments

sense | Sense of the sorting:
SYS_UP | Ascending order
SYS_DOWN | Descending order
lvals | List to be sorted
vals | One-dimensional array to be sorted
cvals | One-dimensional array to be sorted or list of one-dimensional arrays
cmpfct | The name of or a reference to a comparator function of the form function cmpfct(cmpctx,e1,e2):integer that behaves as compare with e1 and e2 of the same type as the array to sort
cmpctx | The value to be passed as the first argument to cmpfct (this parameter is not used by qsort)
ndx | One-dimensional array of the same type and size as the indexing set of vals
lndx | List of the same type as the indexing set of vals
sel | Subset of the indexing set of vals

Example
The following example sorts an array of real numbers:

    declarations
     ar: array(1..10) of real
    end-declarations

    ar:: [1.2, -3, -8, 10.5, 4, 7, 2.9, -1, 0, 5]
    qsort(true, ar)
    writeln("Sorted array: ", ar)

Here we use a comparator function ( cmp) to sort an array of records:

    declarations
      myc=record
       v,w:integer
      end-record
      C: array(1..4) of myc
      LS: list of integer
    end-declarations

    function cmp(b:boolean,i:myc,j:myc):integer
     returned:=compare(i.v,j.v)                ! compare 'v' first
     if returned=0: returned:=compare(i.w,j.w) ! use 'w' to break ties
    end-function

    C::[myc(.v:=1,.w:=5), myc(.v:=3,.w:=4), myc(.v:=3,.w:=3), myc(.v:=2,.w:=3)]
    qsort(SYS_UP, ->cmp, true, C, LS)   ! third argument is not used here
    write("Sorted array: ")
    forall(i in LS) write("(",C(i).v,",",C(i).w,") ")
    writeln                             ! Sorted array: (1,5) (2,3) (3,3) (3,4)

Further information
1. In the first two versions of the procedure (with two arguments, sense and vals or lvals) the input array (list) vals ( lvals) is overwritten by the resulting sorted array (list).
2. When an array ndx is provided, the resulting sorted array is returned in the argument ndx in the form of its sorted index set. If a selection set sel of indices is provided, only the specified indices are processed.
3. When a list lndx is provided, the resulting sorted array is returned in the argument lndx in the form of a list of sorted indices. If a selection set sel of indices is provided, only the specified indices are processed.
4. When applied to a dynamic array this procedure processes all indices of the index set including those not referring to an existing cell (a subset of the indexing set sel can be used to select only the existing entries).
5. The second version of the routine can handle arrays of integers, reals and strings. Other versions also accept module types supporting ordering (like text or date for instance).
6. When the parameter cvals is a list of arrays it is expected that all these arrays have one dimension and are all indiced by the same set. The list can contain up to 10 arrays. When performing the sorting the routine will use the first array values as the primary sorting criteria and then the following array in case of equality.
7. A comparator routine may also be provided in the form of user-defined function cmpfct (the function must be declared public if it is passed by name). The first parameter of this function is given via cmpctx that can be of any scalar type (including a record), it is not used by the qsort algorithm but may be employed by the comparator function to store data required for the comparison. The 2 other arguments, that are of the same type as the collection to sort, are the elements to compare: the function must return 0 if the 2 elements are identical, -1 if the first element is smaller or 1 otherwise. When using this form there is no restriction on the type of the data to sort.
Module
mmsystem
Parent Topic Procedures and functions

© 2001-2026 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.