# Function Reference

# _ArraySort

Sort a 1D or 2D array on a specific index using the dualpivotsort/quicksort/insertionsort algorithms

#include <Array.au3>
_ArraySort ( ByRef $aArray [, $iDescending = 0 [, $iStart = 0 [, $iEnd = 0 [, $iSubItem = 0 [, $iPivot = 0]]]]] )

## Parameters

$aArray | Array to sort
$iDescending | [optional] If set to 1, sort in descending order
$iStart | [optional] Index of array to start sorting (default 0 = first element or row)
$iEnd | [optional] Index of array to stop sorting (default 0 = last element or row)
$iSubItem | [optional] Sub-index to sort on in 2D arrays (default 0 = first column)
$iPivot | [optional] Use pivot sort algorithm (default = quicksort)

## Return Value

Success: | 1.
Failure: | 0 and sets the @error flag to non-zero.
@error: | 1 - $aArray is not an array 2 - $iStart is greater than $iEnd 3 - $iSubItem is greater than subitem count 4 - $aArray is not a 1D or 2D array 5 - $aArray is empty 6 - $iPivot used with 2D array

## Remarks

By default the UDF uses a QuickSort algorithm to sort the array. Setting the $iPivot parameter uses a DualPivotSort algorithm on 1D arrays - this can be significantly faster for large arrays (> 50 elements) - but sorting 2D arrays with this algorithm is very much slower and the UDF will return an error in this case.
In both algorithms, relatively short arrays will be sorted using an insertion sort (< 15 elements with QuickSort; < 45 elements with Dual PivotSort).
Note that there is no guarantee that a specific algorithm will be faster in a given case.
String datatype elements are sorted alphabetically and number datatype elements are sorted numerically - it is important to ensure that elements are of the correct datatype before sorting.

## Example

### Example 1

    ; using a 1D array

    #include <Array.au3>

    Local $avArray[10] = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

    _ArrayDisplay($avArray, "BEFORE _ArraySort()")
    _ArraySort($avArray)
    _ArrayDisplay($avArray, "AFTER QuickSort ascending")
    _ArraySort($avArray, 1)
    _ArrayDisplay($avArray, "AFTER QuickSort descending")
    _ArraySort($avArray, 0, 3, 6)
    _ArrayDisplay($avArray, "AFTER QuickSort from index 3 to 6")
    _ArraySort($avArray, 0, 0, 0, 0, 1)
    _ArrayDisplay($avArray, "AFTER DualPivotSort ascending")

### Example 2

    ; using a 2D array

    #include <Array.au3>

    Local $avArray[5][3] = [ _
            [5, 20, 8], _
            [4, 32, 7], _
            [3, 16, 9], _
            [2, 35, 0], _
            [1, 19, 6]]

    _ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()")
    _ArraySort($avArray, 0, 0, 0, 0)
    _ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 0")
    _ArraySort($avArray, 0, 0, 0, 1)
    _ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 1")
    _ArraySort($avArray, 0, 0, 0, 2)
    _ArrayDisplay($avArray, "$avArray AFTER _ArraySort() ascending column 2")