sort
bool sort(array array, [int flag])
|
array
|
Array to be sorted
|
|
flag
|
Flag specifying how to compare key values (PHP 4.0.0+ only)
|
Sorts an array in ascending order by element value.
Returns:
TRUE on success; FALSE on failure
Description:
Sorts array
in ascending order by element value. String indexes are ignored and the resulting array is indexed numerically. Use asort() if you're working with an associative array. The key values for the resulting indexed array are reordered after the sort is complete.
array
is modified directly by this function.
(PHP 4.0.0+ only) The way in which element values are compared can be modified by passing one of the following named constants as flag
:
|
SORT_REGULAR
|
Compare element values according to PHP's normal comparison rules. This is the default if flag
is not given.
|
|
SORT_NUMERIC
|
Compare element values according to their numeric values.
|
|
SORT_STRING
|
Compare element values according to their string values.
|
Version:
PHP 3, PHP 4
See also:
array_multisort()
arsort()
asort()
krsort()
ksort()
natsort()
natcasesort()
rsort()
uasort()
uksort()
usort()
Example:
Sort an array in ascending order by element value
$array = array('b' => 2, 'd' => 4, 'a' => 1, 'c' => 3);
rsort($array);
print_r($array);
Output (note that string indexes have been lost):
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
|