Easy Tutorial
❮ Func String Strrchr Func Ftp Delete ❯

PHP sort() Function

Complete PHP Array Reference Manual

Example

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

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

Definition and Usage

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

Tip: Use the rsort() function to sort an array in descending order.

Syntax

Parameter Description
array Required. Specifies the array to sort.
sortingtype Optional. Specifies how to arrange the array elements/items. Possible values: 0 = SORT_REGULAR - Default. Sort items normally (Standard ASCII, do not change types).<br> 1 = SORT_NUMERIC - Treat each item as a number.<br> 2 = SORT_STRING - Treat each item as a string.<br> 3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed with setlocale()).<br> 4 = SORT_NATURAL - Treat each item as a string, using natural ordering like natsort().<br> 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, FALSE on failure.
PHP Version: 4+
--- ---

More Examples

Example 1

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


❮ Func String Strrchr Func Ftp Delete ❯