Open In App

PHP natcasesort() Function

Last Updated : 20 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The natcasesort() function is an inbuilt function in PHP which is used to sort an array by using a "natural order" algorithm. The natural order tells the order to be used as a normal human being would use. That is, it does not check the type of value for comparison. For example, in string representation 30 is less than 7 according to the standard sorting algorithm as 3 comes before 7 lexicographically. But in natural order 30 is greater than 7. Also, the natcasesort() function is case insensitive. Syntax:

bool natcasesort($array )

Parameters: This function accepts a single parameter $array. It is the array which natcasesort() function is going to sort. Return Value It returns a boolean value i.e., TRUE on success and FALSE on failure. Below programs illustrate the natcasesort() function in PHP: Program 1

Output:

Standard sorting:
Array
(
    [0] => Gfg12.jpeg
    [2] => Gfg2.jpeg
    [3] => gfg1.jpeg
    [1] => gfg10.jpeg
)

Natural order case insensitive: 
Array
(
    [3] => gfg1.jpeg
    [2] => Gfg2.jpeg
    [1] => gfg10.jpeg
    [0] => Gfg12.jpeg
)

Program 2

Output:

Array
(
    [2] => Gfg1.jpeg
    [4] => Gfg2.jpeg
    [1] => gfg10.jpeg
    [0] => Gfg15.jpeg
    [3] => gfg22.jpeg
)

Reference: http://php.net/manual/en/function.natcasesort.php


Next Article
Practice Tags :

Similar Reads