SortArray

Syntax

SortArray ArrayName[$]() [Order] [From] [To] [SortType] [SortKey]

Remarks

Performs an alphanumeric sort on the elements in the specified array. SortArray is especially useful for sorting arrays that fill list boxes in a custom dialog box. SortArray can sort one-dimensional or two-dimensional arrays; an error occurs
if the specified array has more than two dimensions.

Argument

Explanation

ArrayName[$]()

The one-dimensional or two-dimensional array to be sorted. Arrays with more than two dimensions are not sorted.

Order

The sorting order:

0 (zero) or omitted Ascending

1 Descending

From

Number of the first element to sort. The default is 0 (zero).

To

Number of the last element to sort (must be greater than From).

SortType

The kind of sort to perform (applies only to two-dimensional arrays):

0 (zero) or omitted Sort the "rows" in the array matrix.

1 Sort the "columns" in the array matrix.


Argument

Explanation

SortKey

The number of the row or column to sort by (applies only to two-dimensional arrays): 0 (zero) indicates the first row or column, 1 indicates the second, and so on. The default is 0 (zero).

If SortType is 0 (zero), indicating a row sort, SortKey specifies the column that determines the sort. If SortType is 1, indicating a column sort, SortKey specifies the row that determines the sort.


Here are some examples of SortArray instructions, with descriptions of their effect.

Instruction

Description

SortArray ArrayTest()

Sort all the element(s) in the array ArrayTest() in ascending order, beginning with the element(s) numbered 0 (zero). If the array is two-dimensional, sort the rows of the array matrix, using the first column as the sort key.

SortArray List$(), 0, 1, 10

Sort the elements numbered 1 through 10 in the array List$() in ascending order.

SortArray MailingList$(), 1, 1, 20, 0, 1

Sort the elements numbered 1 through 20 in the two-dimensional array MailingList$() in descending order. Sort rows, using the second column as the sort key.

SortArray Table(), 0, 0, 10, 1, 3

Sort the elements numbered 0 (zero) through 10 in the two-dimensional array Table() in ascending order. Sort columns, using the fourth row as the sort key.


Note

Although the SortArray arguments other than ArrayName[$]() are optional, you cannot omit arguments between arguments that you do include.
For example, SortArray Test(), 0, 0, 2, 0, 1 is a valid instruction, but SortArray Test(), 0, , , , 1 is not valid and will not sort the array.

Examples

This example creates an array containing the names of all the bookmarks in the active document and then sorts the names. When first defined, the variable marks$(0) represents the name of the first bookmark added to the document. After the array is sorted, marks$(0) represents the first name in an alphabetic list of the bookmark names.


size = CountBookmarks() - 1
Dim marks$(size)
For count = 0 To size
    marks$(count) = BookmarkName$(count + 1)
Next
SortArray(marks$())

The following example opens a text file containing a list of 100 names and addresses. There are five fields for the names and addresses: the first field is for the name, the second for the street address, the third for the city or town, the fourth for the state or province, and the fifth for the postal code. The array MailList$() is defined to accommodate the names and addresses, which are read into the array. The array is then sorted by postal code in descending order (so that rows with the highest postal code are first). The sorted names and addresses are then written back to the file. Note that you could perform the same sort by opening the file in a document window and using the TableSort statement.


Open "LIST.TXT" For Input As #1
Dim MailList$(99, 4)
For x = 0 To 99
    Read #1, MailList$(x, 0), MailList$(x, 1), MailList$(x, 2), \
        MailList$(x, 3), MailList$(x, 4)
Next
Close #1
SortArray MailList$(), 1, 0, 99, 0, 4
Open "NEWLIST.TXT" For Output As #1
For x = 0 To 99
    Write #1, MailList$(x, 0), MailList$(x, 1), MailList$(x, 2), \
            MailList$(x, 3), MailList$(x, 4)
Next
Close #1

See Also

Dim, TableSort