Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to sort an array using a case insensitive with natural order algorithm in PHP

1 Answer

0 votes
$arr1 = $arr2 = array('abc0', 'ABC0', 'abc12', 'abc10', 'abc2', 'abc1', 'ABC3');

sort($arr1);
echo "Standard sorting - sort()<br />";
echo "<pre>";
print_r($arr1);
echo "</pre>";

natcasesort($arr2);
echo "<br />Natural order sorting - case-insensitive - natcasesort() <br />";
echo "<pre>";
print_r($arr2);
echo "</pre>";
 
/*
run: 

Standard sorting - sort()

Array
(
    [0] => ABC0
    [1] => ABC3
    [2] => abc0
    [3] => abc1
    [4] => abc10
    [5] => abc12
    [6] => abc2
)

Natural order sorting - case-insensitive - natcasesort() 

Array
(
    [0] => abc0
    [1] => ABC0
    [5] => abc1
    [4] => abc2
    [6] => ABC3
    [3] => abc10
    [2] => abc12
)
 
*/

 



answered Jul 12, 2016 by avibootz
...