sort

Sort an array in ascending order

&reftitle.description; `true` `sort` `array` `array` `int` `flags` `SORT_REGULAR`

Sorts `array` in place by values in ascending order.

¬e.sort-unstable; ¬e.no-key-association; ¬e.reset-index;
&reftitle.parameters;

`array`

- The input array.

&sort.flags.parameter;
&reftitle.returnvalues;

&return.true.always;

&reftitle.changelog;

&Version; | &Description;
&return.type.true;

&reftitle.examples;

### `sort` example

    <?php

    $fruits = array("lemon", "orange", "banana", "apple");
    sort($fruits);
    foreach ($fruits as $key => $val) {
        echo "fruits[" . $key . "] = " . $val . "\n";
    }

    ?>

&example.outputs;

    fruits[0] = apple
    fruits[1] = banana
    fruits[2] = lemon
    fruits[3] = orange

The fruits have been sorted in alphabetical order.

### `sort` example using case-insensitive natural ordering

    <?php

    $fruits = array(
        "Orange1", "orange2", "Orange3", "orange20"
    );
    sort($fruits, SORT_NATURAL | SORT_FLAG_CASE);
    foreach ($fruits as $key => $val) {
        echo "fruits[" . $key . "] = " . $val . "\n";
    }

    ?>

&example.outputs;

    fruits[0] = Orange1
    fruits[1] = orange2
    fruits[2] = Orange3
    fruits[3] = orange20

The fruits have been sorted like `natcasesort`.

&reftitle.notes;

Like most PHP sorting functions, `sort` uses an implementation of Quicksort. The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays. This is however an implementation detail you shouldn't rely on.

Be careful when sorting arrays with mixed types values because `sort` can produce unexpected results, if `flags` is `SORT_REGULAR`.

&reftitle.seealso;

- `rsort`
- &seealso.array.sorting;