PHP sort() Function

What is PHP sort() Function?

If you want to sort element values of an array in ascending order meaning smallest to largest, use sort() function.

Syntax:

sort(array, flag)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

array (Required): it specifies an array. Check the example 1.

flag (Optional): It specifies a constant that describes how to compare array items. It has 6 possible values-

  • SORT_REGULAR – it is the default value. It doesn’t change data type of elements if there exists mixed data types (ex. string, numbers) and it uses standard PHP comparison rules. Note, it may lead unexpected results. Check example 2.
  • SORT_NUMERIC – It compare array elements as numeric. Use this when you want to sort an array that has both numeric string and integer. Check example 3.
  • SORT_STRING – It compare array elements as strings. Check example 4.
  • SORT_LOCALE_STRING – It compares array elements as strings based on currently set locale. You must set the locale using setlocale()  function before to get correct result from SERT_LOCALE_STRING flag. Check example 5.
  • SORT_NATURAL – It compares array elements as string using natural ordering like a human. So, “version8” comes before “version10”. Check example 6.
  • SORT_FLAG_CASE – It compares case-insensitively array elements as string using natural ordering like a human. So, “Version10” comes before “version1”. It can be used using | (bitwise operator) with SORT_NATURAL or SORT_STRING. Check example 7.

Return Values:

This function always returns TRUE. Note: the function doesn’t return the sorted array, instead, it modifies the original array by changing the element order.

Examples:

Example 1:

<pre>
<?php
$array = array(300, 200, 400, 100);
sort($array); 
print_r($array);
?>
</pre>

Output:

Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)

Example 2:

<pre>
<?php
$array = array(300, 200, 400, 100, "PHP", "MySQL");
sort($array, SORT_REGULAR); 
print_r($array);
?>
</pre>

Output:

Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
[4] => MySQL
[5] => PHP
)

Example 3:

<pre>
<?php
$array = array("500", "200", "400", "1001", "100");
sort($array, SORT_NUMERIC);
print_r($array);
?>
</pre>

Output:

Array
(
[0] => 100
[1] => 200
[2] => 400
[3] => 500
[4] => 1001
)

Example 4:

<pre>
<?php
$array = array("500", "200", "400", "1001", "100");
sort($array, SORT_STRING);
print_r($array);
?>
</pre>

Output:

Array
(
[0] => 100
[1] => 1001
[2] => 200
[3] => 400
[4] => 500
)

Example 5:

<pre>
<?php /* The exampla below sorts the Ukrainian strings in ascending order.*/
setlocale(LC_ALL, 'uk_UA.UTF-8', 'uk_UA.utf8', 'uk_UA', 'ukr_UKR', 'ukr');
$array = array("один", "два", "три", "чотири");
sort($array, SORT_LOCALE_STRING);
print_r($array);
?>
</pre>

Output:

Array
(
[0] => два
[1] => один
[2] => три
[3] => чотири
)

Example 6:

<pre>
<?php
$array_standard = array("version10", "version7", "version8.5", "version1");
$array_natural  = array("version10", "version7", "version8.5", "version1");

sort($array_standard);
echo "After Standard Sorting:\n";
print_r($array_standard);

sort($array_natural, SORT_NATURAL);
echo "After Natural Sorting:\n";
print_r($array_natural);
?>
</pre>

Output:

After Standard Sorting:
Array
(
[0] => version1
[1] => version10
[2] => version7
[3] => version8.5
)
After Natural Sorting:
Array
(
[0] => version1
[1] => version7
[2] => version8.5
[3] => version10
)

Example 7:

<pre>
<?php
$array_standard = array("Version10", "version7", "Version8.5", "version1");
$array_natural  = array("Version10", "version7", "Version8.5", "version1");

sort($array_standard);
echo "After Standard Sorting:\n";
print_r($array_standard);

sort($array_natural, SORT_NATURAL | SORT_FLAG_CASE);
echo "After Natural case-insensitive Sorting:\n";
print_r($array_natural);
?>
</pre>

Output:

After Standard Sorting:
Array
(
[0] => Version10
[1] => Version8.5
[2] => version1
[3] => version7
)
After Natural case-insensitive Sorting:
Array
(
[0] => version1
[1] => version7
[2] => Version8.5
[3] => Version10
)

Notes on sort() Function:

  • The function reindexes the keys of the sorted array. So, it doesn’t preserve the keys of the original array.
  • If multiple elements have same values, the function doesn’t change their order.
  • After sorting, the function set the internal pointer to the first element in the array.
  • The function removes associative keys and changes these to numerical keys.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP sort() Function

The sort() function is a built-in PHP function and part of the PHP’s array functions. It is a quick way to sort array elements in ascending order.

Reference:

https://www.php.net/manual/en/function.sort.php