# Array sort Method

Syntax

Arguments

- optionsCharacter
Specifies how to sort the values. Default value is "A" (ascending). The following Flag values are possible:

- Flag
- Meaning
- A
Ascending

- D
Descending

- C
Case sensitive sorting (if character data)

- B
Sort blank character values after non-blank values. This option is useful when you are sorting character values in ascending order, in an array that has more entries allocated than are used. The blank entries will sort to the bottom of the array.

- subkeyCharacter
A modifier that allows for sorting on sub-strings or on arrays of properties. In the expression, you can use the special system term "value" to refer to an array value.

- startNumeric
Default = <array>.lower(). The index of the first element to include in the sort.

- endNumeric
Default = <array>.upper(). The index of the last element to include in the sort.

Description

Sort the array.

Discussion

The <array>.sort() method sorts a single dimensional array.

For example, to sort an array called "fullnames" on the second word of the array entry, use this expression:

fullnames.sort("A", "word(value,2)")

To understand the above command, visualize the "fullnames" array as follows:

Element | Value | word(value,2) |
---|---|---|

fullnames[1] | John Smith | Smith |

fullnames[2] | Ken Jones | Jones |

fullnames[3] | Karen Able | Able |

Sorting the array using word(value,2) gives the following result:

Element | Value | word(value,2) |
---|---|---|

fullnames[3] | Karen Able | Able |

fullnames[2] | Ken Jones | Jones |

fullnames[1] | John Smith | Smith |

### Sorting Property Arrays

For property arrays, the expression parameter refers to the property name. For example, the following will sort the array on the mom property:

dim ar[5] as P ar[1].mom = "Irene" ar[1].dad = "Abe" ar[2].mom = "Arlene" ar[2].dad = "Kyle" ar.sort("A","mom")

To understand the above command, visualize the ar array as follows:

Element | Mom | Dad |
---|---|---|

ar[1] | Irene | Abe |

ar[2] | Arlene | Kyle |

Sorting on the mom property will order the array entries alphabetically in ascending order using the value in mom:

Element | Mom | Dad |
---|---|---|

ar[2] | Arlene | Kyle |

ar[1] | Irene | Abe |

If the optional start and end parameters are omitted, the whole array is sorted. If these parameters are supplied, only the array entries in the range start to end are sorted. For example:

dim numArr2[10] as N = floor(rand()*100)+2 ? numArr2 = [1] = 89 [2] = 32 [3] = 95 [4] = 77 [5] = 59 [6] = 18 [7] = 19 [8] = 3 [9] = 92 [10] = 40 numArr2.sort("A","",3,7) ? numArr2 = [1] = 89 [2] = 32 [3] = 18 ' elements 3 [4] = 19 ' through 7 [5] = 59 ' are sorted [6] = 77 ' in ascending [7] = 95 ' order [8] = 3 [9] = 92 [10] = 40

### Sorting an Array with Multiple Properties

Assume you have an array with the following data and you want to sort by multiple properties. The second argument of the function is simply an expression based on property names.

dim ar[5] as P ar[1].name = "Aaron" ar[1].age = 20 ar[2].name = "David" ar[2].age = 10 ar[3].name = "Charles" ar[3].age = 13 ar[4].name = "Aaron" ar[4].age = 50 ar[5].name = "Betty" ar[5].age = 10 ar.sort("A","name + age") ? ar[1].name = "Aaron" ? ar[2].name = "Aaron" ? ar[3].name = "Betty" ar.sort("A", "age + name") ? ar[1].name = "Betty" ? ar[2].name = "David" ? ar[3].name = "Aaron"

### Sorting One Array Based on Values in another Array

The following example shows how you can sort the values in one array based on the sort order defined by the values in another parallel array.

You can follow the example by typing these commands in the Interactive window.

dim base_array[4] as C dim parallel_array[4] as C base_array[1] = "dog" base_array[2] = "rat" base_array[3] = "cat" base_array[4] = "bat" parallel_array[1] = "z" parallel_array[2] = "a" parallel_array[3] = "b" parallel_array[4] = "d" base_array.sort("", "parallel_array[value]") ? base_array = [1] = "rat" [2] = "cat" [3] = "bat" [4] = "dog" 'now sort in random order base_array.sort("","str(rand())")

### Example

Assume you have the following array:

dim a[5] as C a[1] = "orange" a[2] = "banana" a[3] = "apple" a[4] = "" a[5] = ""

The following table shows how different commands will sort the array.

Element | A.sort("A") | A.sort("D") | A.sort("AB") |
---|---|---|---|

A[1] | Orange | Apple | |

A[2] | Banana | Banana | |

A[3] | Apple | Apple | Orange |

A[4] | Banana | ||

A[5] | Orange |