PHP » Arrays » sort()

Syntax:
bool sort (array array [, int sort_type])
array
Array to be sorted
sort_type
Can be SORT_REGULAR, SORT_NUMERIC, or SORT_STRING.

Sorts an array in ascending order.

The sort() function sorts an array in ascending order. Any keys in the array will be lost. The optional sort_type parameter specifies whether the elements should be sorted as strings or numerically. If the array was sorted successfully, TRUE is returned.

Examples

Code:
function elementPrint($value)
{
   print "$value ";
}

$array1 = array(34, 75, 23, 68, 54, 91, 24, 12);
$array2 = array("orange", "banana", "apple", "plum", "pear");
$array3 = array(23, 34, 12, 19, 2, "4");
$array4 = array(23, 34, "hello", 19, 2, "4");

sort($array1);
array_walk($array1, "elementPrint");
print "<br>";

sort($array2);
array_walk($array2, "elementPrint");
print "<br>";

sort($array3);
array_walk($array3, "elementPrint");
print "<br>";

sort($array4, SORT_NUMERIC);
array_walk($array4, "elementPrint");
print "<br>";

sort($array4, SORT_STRING);
array_walk($array4, "elementPrint");
print "<br>";

?>
Output:
12 23 24 34 54 68 75 91
apple banana orange pear plum
2 4 12 19 23 34
hello 2 4 19 23 34
19 2 23 34 4 hello
Explanation:

String, numeric, and mixed arrays are sorted in a few different ways.

See Also: