PHP sort()Functions

PHP Array ReferenceComplete PHP Array Reference

Examples

Sort the elements of the array $cars in alphabetical ascending order:

<?php $cars=array("Volvo","BMW","Toyota"); sort($cars); ?>

Run example »

Definition and Usage

The sort() function sorts an indexed array in ascending order.

Tip:Please usersort()function to sort numeric arrays in descending order.

Syntax

sort(array,sortingtype);

Parameter Description
array Required. Specifies the array to sort.
sortingtype Optional. Specifies how to arrange the elements/items of the array. Possible values:
  • 0 = SORT_REGULAR - Default. Compare items normally (Standard ASCII, without changing types).
  • 1 = SORT_NUMERIC - Compare items numerically.
  • 2 = SORT_STRING - Compare items as strings.
  • 3 = SORT_LOCALE_STRING - Compare items as strings, based on the current locale (can besetlocale()changed).
  • 4 = SORT_NATURAL - Compare items as strings, using something likenatsort()natural ordering.
  • 5 = SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.

Technical Details

Return value: Returns TRUE on success or FALSE on failure.
PHP Version: 4+


More Examples

Example 1

Sort the elements of the array $numbers in ascending numeric order:

<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
?>

Run example »


PHP Array ReferenceComplete PHP Array Reference Other Extensions