English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP sort() Function Usage and Example

PHP Array Function Manual

The sort() function sorts an array

Syntax

sort($array[, $sort_flags]);

Definition and Usage

This function sorts an array. After this function is executed, the elements will be arranged from lowest to highest.

Parameter

Serial NumberParameters and Description
1

array(Required)

It specifies an array.

2

sort_flags(Optional)

It specifies how to sort array values. Possible values-

  • SORT_REGULAR - Default. Compares elements normally (without changing type)

  • SORT_NUMERIC - Elements are compared as numbers

  • SORT_STRING - Elements are compared as strings

  • SORT_LOCALE_STRING - Compares elements as strings based on the current locale setting. You can change the locale with setlocale().

  • SORT_NATURAL - Similar to natsort(), sorts each element in 'natural order'. PHP 5.4Added in version .0.

  • SORT_FLAG_CASE - Can be combined with SORT_STRING or SORT_NATURAL (OR bitwise operation), case-insensitive sorting of strings.

Return Value

Returns TRUE on success, FALSE on failure.

Online Example

<?php
   $input = array("d"=>"lemon", "a"=>"orange", "b"=>"banana");
   sort($input);
   print_r($input);
?>
Test and See‹/›

Output Result:

Array(
   [0]=>banana
   [1]]=>lemon
   [2]]=>orange
)

PHP Array Function Manual