168 of 410 menu

The sort Function

The sort function sorts an array in ascending order by its elements. The function modifies the array itself.

Syntax

sort(array &$array, int $flags = SORT_REGULAR): bool

Example

Let's sort an array in ascending order by its elements:

<?php $arr = [1, 3, 2, 5, 4]; sort($arr); var_dump($arr); ?>

The code execution result:

[1, 2, 3, 4, 5]

See Also

  • the rsort function,
    which sorts in descending order by elements
  • the ksort function,
    which sorts in ascending order by keys
  • the krsort function,
    which sorts in descending order by keys
  • the asort function,
    which sorts in ascending order by elements with key preservation
  • the arsort function,
    which sorts in descending order by elements with key preservation
  • the natsort function,
    which sorts using natural order
  • the natcasesort function,
    which sorts using natural order case-insensitively
  • the usort function,
    which sorts using a callback function
  • the uksort function,
    which sorts by keys using a callback function
  • the uasort function,
    which sorts using a callback function with key preservation
  • the array_multisort function,
    which sorts multiple arrays
byenru