natsort


php128 apg

SORT an ARRAY using a case sensitive natural order algorithm.





This function returns TRUE on success or FALSE on failure.

This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations as decribed in the natural order algorthm - very close to human understanding.

If two members compare as equal, their relative order in the sorted ARRAY is undefined.

UPPERCASE and lower case are considered ANTAGONIC entities.

UPPERCASE ordering is prioritized.



<?php

bool natsort 
arr &$array )
 

where,

&
$array The input ARRAY


?> 

&$array


The input ARRAY.



  1 EXERCISE   

<?php

$arr01n 
= [ 'BLUE''Blue''blue'
                      
'Green''green''GREEN''wHiTe' ];

echo 
'The given ARRAY:<br>';
print_r($arr01n);

echo 
'<br><br>';

if(
natsort($arr01n) == true)
{
echo 
'The SORTED ARRAY:<br>';
print_r($arr01n);
}
else
{
echo 
'The array can not be sorted!';
}

?> 

 RESULT   

The given ARRAY:
Array
(
[0] => BLUE
[1] => Blue
[2] => blue
[3] => Green
[4] => green
[5] => GREEN
[6] => wHiTe
)

The SORTED ARRAY:
Array
(
[0] => BLUE
[1] => Blue
[5] => GREEN
[3] => Green
[2] => blue
[4] => green
[6] => wHiTe
)


  2 EXERCISE   

<?php

$arr02n 
= [ '0' => 'ONE',  'T' => 'TWO'
                                      
't' => 'three''F' => 'FOUR' ];

echo 
'The given ARRAY:<br>'
print_r($arr02n);

echo 
'<br><br>';

if(
natsort($arr02n) == true)
{
echo 
'The SORTED ARRAY:<br>'
print_r($arr02n);
}
else
{
echo 
'The array can not be sorted!';
}

?> 

 RESULT   

The given ARRAY:
Array
(
[0] => ONE
[T] => TWO
[t] => three
[F] => FOUR
)

The SORTED ARRAY:
Array
(
[F] => FOUR
[0] => ONE
[T] => TWO
[t] => three
)


  3 EXERCISE   

<?php

$arr03n 
= [ => 'São Paulo'=> 'Santo André'
                   
=> 'São Caetano'=> 'Marília' ];

echo 
'The given ARRAY:<br>'
print_r($arr03n);

echo 
'<br><br>';

if(
natsort($arr03n) == true)
{
echo 
'The SORTED ARRAY:<br>'
print_r($arr03n);
}
else
{
echo 
'The array can not be sorted!';
}

?> 

 RESULT   

The given ARRAY:
Array
(
[1] => São Paulo
[2] => Santo André
[3] => São Caetano
[4] => Marília
)

The SORTED ARRAY:
Array
[4] => Marília
[2] => Santo André
[3] => São Caetano
[1] => São Paulo
)


The result of this exercise can be different on your system.


  4 EXERCISE   

<?php

setlocale
(LC_ALL"pt_BR.utf-8""pt-BR");

// setlocale(LC_ALL, "en_US", "en-US"); 

$arr04n = [=> 33.33=> 2.456=> M_PI];

echo 
'The given ARRAY:<br>'
print_r($arr04n);

echo 
'<br><br>';

if(
natsort($arr04n) == true)
{
echo 
'The SORTED ARRAY:<br>'
print_r($arr04n);
}
else
{
echo 
'The array can not be sorted!';
}

?> 

 RESULT   

This function is no longer compatible
with LOCALE in PHP 8.0.XX,
however, it is still compatible in PHP 7.4.XX.


You should verify this by running this exercise in both versions, of course, if possible.


  5 EXERCISE   

<?php

/*
* Function is implemented 
* in ext/standard/array.c
*/

$array1 $array2 = [ "img12.png"
                                 
"img10.png"
                                              
"img2.png"
                                                     
"img1.png" ];

echo 
'The given ARRAY:<br>'
print_r($array1);
sort($array1);
echo 
"<br><br>Standard sorting:<br>";
print_r($array1);
natsort($array2);
echo 
"<br><br>Natural order sorting:<br>";
print_r($array2);

?>

 RESULT   

The given ARRAY:
Array
(
[0] => img12.png
[1] => img10.png
[2] => img2.png
[3] => img1.png
)


Standard sorting:
Array
(
[0] => img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)



Natural order sorting:
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)