Commands and Statements

Command SORT Foundation

Copies sorted records to a new file.

SORT TO <cFilename> ;
     ON <cFieldname> [ [/A | /D] [/C] ] ;
     [, <cFieldname> [ [/A | /D] [/C] ] ... ] ;
[FOR    <lForCondition>] ;
[WHILE  <lWhileCondition>] ;
[NEXT   <nCount>] ;
[RECORD <xRecordID>] ;
[REST] ;
[FIELDS FieldNameList | FIELDS LIKE Skeleton | FIELDS EXCEPT Skeleton]
<cFilename> specifies the name of the file where the sorted data is written. The name must contain the drive and path, if necessary. It can be specified either as a literal file name or as a character expression in parentheses. When the file name is specified without a file extension, the default extension predetermined by the current database engine (DBE) is used. The default file extension of the DBFDBE is ".DBF".
<cFieldname> is the name of a field variable used to sort the data copied into the file <cFilename>. A list of fields can be specified. Field names can be specified as literals or as character expressions in parentheses.
By default, the sorting order is ascending (A->Z). If the switch /D follows the field name (field name /D), this field is sorted in descending order (Z->A). The switch /C can be used for character fields to indicate a sort that is case insensitive. The switch /A indicates an ascending order sort (the default).
<lForCondition> is an optional logical expression which sets a condition. Only records for which <lForCondition> returns the value .T. (true) are written to the new file.
<lWhileCondition> is an optional logical expression which sets a condition. Sorting is terminated as soon as the expression returns the value .F. (false).
<nCount> optionally specifies the number of records written to the new sorted file, starting with the current record.
The option REST specifies whether records are copied to the sorted new file only from the current record to the last record. If a condition is specified, the option ALL is used as the default value.
The option ALL specifies that all records are written to the new sorted file. If a condition is specified, the condition is tested for all records.
FIELDS <FieldNameList>
This command option is ignored in Xbase++ 2.0
This command option is ignored in Xbase++ 2.0

The file command SORT copies the records from a work area to a target file in a sorted order. The target file must not be open prior to calling the procedure. If the file already exists, it is overwritten without warning.

The function only sorts by fields and not by expressions. This means at least one field name <cFieldname> must be specified. Memo fields cannot be sorted. Character fields are sorted according to their ASCII value, numeric fields according to numeric value and date fields are sorted chronologically. For logical fields, the lesser value is .F. (false).

The sort range can be limited by specifying conditions or the number of records to sort. If SET DELETED is set to ON, records marked for deletion are not copied. Filtered records are also ignored if a filter condition is active.

In network environment, the source file in the work area must be locked or exclusively open.

The functional equivalent of SORT is the function DbSort().

// In the example, a sorted telephone list of employees 
// who work in a specific department is created, based on 
// a personnel database. 



   SORT TO Temp ; 
        ON LName, FName, PhoneNo ; 
       FOR FIELD->DeptNo = "S-DEV" 

   USE Temp 


   CLOSE Temp 


If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.