PHP sort is an inbuilt function in PHP. It sorts an array in ascending order i.e, from smaller to bigger element. Also, it makes the changes in the original array itself. The function provides 6 sorting types to sort an input array. In this article, we will discuss the PHP sort Function.
In order to sort an array in descending order, you can use PHP rsort Function.
Also, like most PHP sorting Functions, it uses an implementation of Quicksort. The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays.
boolean sort($array, $sortingType = SORT_REGULAR)
The PHP sort Function expects two parameters. However, only the first parameter is mandatory.
- $array: The first parameter to the function is an array which you need to sort. Also, it is a mandatory parameter.
- $sortingType: The function provides 6 sorting types which are optional to pass. This parameter can take the following values:
- SORT_REGULAR: On passing SORT_REGULAR or 0, the function compares the elements normally without changing their type. Also, this is the default value to the parameter.
- SORT_NUMERIC: On passing SORT_NUMERIC or 1, the function compares the elements numerically.
- SORT_STRING: The function compares the elements string-wise on passing SORT_STRING or 2.
- SORT_LOCALE_STRING: The function compares the elements as a string based on current locale on passing SORT_LOCALE_STRING or 3. It uses the locale which can be changed by the
- SORT_NATURAL: On passing 4 or SORT_NATURAL, the function compares the elements as strings using natural ordering.
- SORT_FLAG_CASE: On passing 5 or SORT_FLAG_CASE, the function compares the elements as case-insensitive strings. Also, this parameter can be used | (bitwise) with SORT_NATURAL or SORT_STRING.
The PHP sort Function returns true on success and false on failure. However, it modifies the original input array.
Note: The function assigns new keys to the elements in the array. It will remove any existing keys rather than re-ordering.
Let’s discuss a few examples of using the sort Function in PHP.
Example 1: Sort Integer Array in Ascending Order
<?php $array = array(5, 1, 2, 7, 3); sort($array); print_r($array); /* Array (  => 1  => 2  => 3  => 5  => 7 ) */ ?>
You can sort an integer array in ascending order by passing it to the sort function. Also, it modifies the original array itself.
Example 2: Sort Strings in Ascending Order
Similarly, you can sort an array containing strings.
<?php $array = array('banana', 'cat', 'apple'); sort($array); print_r($array); /* Array (  => apple  => banana  => cat ) */ ?>
Example 3: Case Sensitivity in Strings
You can observe the difference in the output when SORT_FLAG_CASE is passed along with SORT_STRING in the below example:
<?php $array = array("concatly", "Ravi", "striver", "codeblocks", "RAvi"); //Function sorts case sensitively sort($array, SORT_STRING); print_r($array); /* Array (  => RAvi  => Ravi  => codeblocks  => concatly  => striver ) */ //Function sorts case insensitively sort($array, SORT_STRING | SORT_FLAG_CASE); print_r($array); /* Array (  => codeblocks  => concatly  => RAvi  => Ravi  => striver ) */ ?>
Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.