Sorts the elements in a one-dimensional array or the rows in a two-dimensional array. Returns 1 if successful; generates an error if unsuccessful.

Syntax

<oRef>.sort([<starting element expN> [,<elements to sort expN> [, <sort order expN>]]])

<oRef>

A reference to the array you want to sort.

<starting element expN>

In a one-dimensional array, the number of the element in <oRef> at which you want to start sorting. In a two-dimensional array, the number (subscript) of the column on which you want to sort. Without <starting element expN>, sort( ) starts sorting at the first element or column in the array.

<elements to sort expN>

In a one-dimensional array, the number of elements you want to sort. In a two-dimensional array, the number of rows to sort. Without <elements to sort expN>, sort( ) sorts the rows starting at the row containing element <starting element expN> to the last row. If you want to specify a value for <elements to sort expN>, you must also specify a value for <starting element expN>.

<sort order expN>

The sort order:

0 specifies ascending order (the default)

1 specifies descending order

If you want to specify a value for <sort order expN>, you must also specify values for <elements to sort expN> and <starting element expN>.

Property of

Array

Description

sort( ) requires that all the elements on which you’re sorting be of the same data type. The elements to sort in a one-dimensional array must be of the same data type, and the elements of the column by which rows are to be sorted in a two-dimensional array must be of the same data type.

sort( ) arranges elements in alphabetical, numerical, chronological, or logical order, depending on the data type of <starting element expN>. (For strings, the current language driver determines the sort order.)

One-dimensional arrays

Suppose you create an array with the following statement:

aNums = {5, 7, 3, 9, 4, 1, 2, 8}

That creates an array with the elements in this order:

5 7 3 9 4 1 2 8

If you call aNums.sort(1, 5), dBASE Plus sorts the first five elements so that the array elements are in this order:

3 4 5 7 9 1 2 8

If you then call aNums.sort(5, 2), dBASE Plus sorts two elements starting at the fifth element so that the array elements are now in this order:

3 4 5 7 1 9 2 8

Two-dimensional arrays

Using sort( ) with a two-dimensional array is similar to using the SORT command with a table. Array rows correspond to records, and array columns correspond to fields.

When you sort a two-dimensional array, whole rows are sorted, not just the elements in the column where <starting element expN>) is located.

For example, suppose you create the array aInfo and fill it with the following data:

Sep 15 1965

7

A

Dec 31 1965

4

D

Jan 19 1945

8

C

May 2 1972

2

B

If you call aInfo.sort(1), dBASE Plus sorts all rows in the array beginning with element number 1. The rows are sorted by the dates in the first column because element 1 is a date. The following figure shows the results.

image\AALPHA7_shg.gif

If you then call aInfo.sort(5, 2), dBASE Plus sorts two rows in the array starting with element number 5, whose value is 7. sort( ) sorts the second and the third rows based on the numbers in the second column. The following figure shows the results.

image\AALPHA8_shg.gif