!SORT()

!SORT()

Top  Previous  Next

 

The !SORT() subroutine sorts the elements of a dynamic array according to a specified sorting rule.

 

 

Format

 

CALL !SORT(in.list, out.list, sort.rule)

 

where

 

in.listis the dynamic array containing the items to be sorted. Any mark character (or a mix of different mark characters) may be used to separate the items.

 

out.listis the variable to receive the sorted dynamic array. The items will be separated by field marks.

 

sort.ruledefines the manner of sorting. This is a string containing characters from the following:

Sort style:

FSort as right aligned values, comparing numeric values as floating point values
LSort as left aligned values (default)
RSort as right aligned values, comparing numeric values as integers
SSimilar to a compound sort but elements that are numerically equivalent values such as "28" and "028" as treated as being different.
XCompound sort for mixed numeric and non-numeric data (see Sorting)

 

Sort order:

ASort in ascending order (default)
DSort in descending order

 

Sort options:

CCase insensitive sort
NIgnore null elements
UReturn unique items. Multiple occurrences of an item are replaced by just one item.

 

Invalid or conflicting sort.rule elements are ignored.

 

 

The !SORT() subroutine sorts elements of in.list into the order defined by sort.rule, returning the sorted list in out.list. The value of in.list is not changed unless it refers to the same variable as out.list.

 

Right aligned sorts should normally be used when sorting numeric data.

 

 

Example

 

CUSTOMER.LIST = ""

SELECT INVOICES

LOOP

  READNEXT ID ELSE EXIT

  READ INVOICE.REC FROM INVOICES, ID THEN

     CUSTOMER.LIST<-1> = INVOICE.REC<CUSTOMER.NAME>

  END

REPEAT

CALL !SORT(CUSTOMER.LIST, CUSTOMER.LIST, "AU")

 

The above program fragment reads all the records from the INVOICE file and builds a list of customer names. This is then sorted, removing duplicates.

 

This approach will be faster than using LOCATE and INS to build a sorted list unless there are a very large number of duplicates.

 

 

See also:
Sorting, SORT.COMPARE()