String_Manipulation / Sorting_Strings


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:

<?php

$arr1 
$arr2 = array("img12.png","img10.png","img2.png","img1.png");
echo 
"Standard string comparisonn";
usort($arr1,"strcmp");
print_r($arr1);
echo 
"nNatural order string comparisonn";
usort($arr2,"strnatcmp");
print_r($arr2);
?>


/* Output
Standard string comparison
Array
(
    [0] => img1.png
    [1] => img10.png
    [2] => img12.png
    [3] => img2.png
)

Natural order string comparison
Array
(
    [0] => img1.png
    [1] => img2.png
    [2] => img10.png
    [3] => img12.png
)
*/

Description

  This code implements a comparison algorithm that orders alphanumeric strings in the way a human being would, this is described as a "natural ordering"