Page 1 of 1

sorting question...

Posted: Tue Sep 02, 2003 3:07 pm
by zolo44
I have a question for you guys... I have an array with over 100 strings. These string have numbers in them. Is there a function that will sort them out? I don't mean a regular sort, bucause on a regular sort, the 100's are on top with the 1's, for exameple:

Array
(
[0] => tn_Misha-Set1-100_jpg.jpg
[1] => tn_Misha-Set1-101_jpg.jpg
[2] => tn_Misha-Set1-102_jpg.jpg
[3] => tn_Misha-Set1-103_jpg.jpg
[4] => tn_Misha-Set1-104_jpg.jpg
[5] => tn_Misha-Set1-105_jpg.jpg
[6] => tn_Misha-Set1-106_jpg.jpg
[7] => tn_Misha-Set1-107_jpg.jpg
[8] => tn_Misha-Set1-108_jpg.jpg
[9] => tn_Misha-Set1-10_jpg.jpg
[10] => tn_Misha-Set1-11_jpg.jpg
[11] => tn_Misha-Set1-12_jpg.jpg
[12] => tn_Misha-Set1-13_jpg.jpg
[13] => tn_Misha-Set1-14_jpg.jpg
[14] => tn_Misha-Set1-15_jpg.jpg
[15] => tn_Misha-Set1-16_jpg.jpg
[16] => tn_Misha-Set1-17_jpg.jpg
[17] => tn_Misha-Set1-18_jpg.jpg
[18] => tn_Misha-Set1-19_jpg.jpg
[19] => tn_Misha-Set1-1_jpg.jpg
[20] => tn_Misha-Set1-20_jpg.jpg
[21] => tn_Misha-Set1-21_jpg.jpg
[22] => tn_Misha-Set1-22_jpg.

I'm looking for a function that will put the 100's in it's place (at the bottom). Anybody have any ideas? I've looked at php.net, but couldn't find anything. :(

Thanks,

Posted: Tue Sep 02, 2003 3:25 pm
by sbe
Try this:

Code: Select all

<?php
function myNatCmp($str1, $str2)
{
$str1 = str_replace("tn_Misha-Set1", "", $str1);
$str1= str_replace("_jpg.jpg", "", $str1);
$str2 = str_replace("tn_Misha-Set1", "", $str2);
$str2= str_replace("_jpg.jpg", "", $str2);

return strnatcmp($str1, $str2);
}

$array = usort($array, "myNatCmp");
?>

Posted: Tue Sep 02, 2003 4:06 pm
by volka
take a look at http://php.net/natsort

Posted: Tue Sep 02, 2003 4:21 pm
by zolo44
Thanks guys. that's exactly what i needed.