Re: [PHP] Tricky sorting problem
the -5 should be -7 René Fournier wrote: (Well, at least it's tricky for me... :-) I need to sort an array based on three numbers that are situated in the middle of each array element. For example: $list[] is an array, and contains the following [unsorted] elements: Super-S862-030.jpg Super-S1162-040.jpg Super-S874-010.jpg Super-S1174-020.jpg I want to sort $list based on those last three numbers, just before the extension, so that the new sort order is: Super-S874-010.jpg Super-S1174-020.jpg Super-S862-030.jpg Super-S1162-040.jpg I tried writing my own sort function--but can't seem to get anywhere with it: function cmp ($a,$b) { if (substr($a,-5,2) == substr($b,-5,2)) return 0; return (substr($a,-5,2) substr($b,-5,2)) ? -1 : 1 ; } usort($list,cmp); Any ideas? (Thanks.) ...Rene --- René Fournier, [EMAIL PROTECTED] Toll-free +1.888.886.2754 Tel +1.403.291.3601 Fax +1.403.250.5228 www.smartslitters.com SmartSlitters International #33, 1339 - 40th Ave NE Calgary AB T2E 8N6 Canada -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Tricky sorting problem
...and the 2 should be 3 :) - E Marek Kilimajer [EMAIL PROTECTED] wrote: the -5 should be -7 René Fournier wrote: (Well, at least it's tricky for me... :-) I need to sort an array based on three numbers that are situated in the middle of each array element. For example: $list[] is an array, and contains the following [unsorted] elements: Super-S862-030.jpg Super-S1162-040.jpg Super-S874-010.jpg Super-S1174-020.jpg I want to sort $list based on those last three numbers, just before the extension, so that the new sort order is: Super-S874-010.jpg Super-S1174-020.jpg Super-S862-030.jpg Super-S1162-040.jpg I tried writing my own sort function--but can't seem to get anywhere with it: function cmp ($a,$b) { if (substr($a,-5,2) == substr($b,-5,2)) return 0; return (substr($a,-5,2) substr($b,-5,2)) ? -1 : 1 ; } usort($list,cmp); Any ideas? (Thanks.) ...Rene --- René Fournier, [EMAIL PROTECTED] Toll-free +1.888.886.2754 Tel +1.403.291.3601 Fax +1.403.250.5228 www.smartslitters.com SmartSlitters International #33, 1339 - 40th Ave NE Calgary AB T2E 8N6 Canada -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Tricky sorting problem
Yes, and 2 should be 3 Marek Kilimajer wrote: the -5 should be -7 René Fournier wrote: (Well, at least it's tricky for me... :-) I need to sort an array based on three numbers that are situated in the middle of each array element. For example: $list[] is an array, and contains the following [unsorted] elements: Super-S862-030.jpg Super-S1162-040.jpg Super-S874-010.jpg Super-S1174-020.jpg I want to sort $list based on those last three numbers, just before the extension, so that the new sort order is: Super-S874-010.jpg Super-S1174-020.jpg Super-S862-030.jpg Super-S1162-040.jpg I tried writing my own sort function--but can't seem to get anywhere with it: function cmp ($a,$b) { if (substr($a,-5,2) == substr($b,-5,2)) return 0; return (substr($a,-5,2) substr($b,-5,2)) ? -1 : 1 ; } usort($list,cmp); Any ideas? (Thanks.) ...Rene --- René Fournier, [EMAIL PROTECTED] Toll-free +1.888.886.2754 Tel +1.403.291.3601 Fax +1.403.250.5228 www.smartslitters.com SmartSlitters International #33, 1339 - 40th Ave NE Calgary AB T2E 8N6 Canada -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Tricky sorting problem
I need to sort an array based on three numbers that are situated in the middle of each array element. For example: $list[] is an array, and contains the following [unsorted] elements: Super-S862-030.jpg Super-S1162-040.jpg Super-S874-010.jpg Super-S1174-020.jpg I want to sort $list based on those last three numbers, just before the extension, so that the new sort order is: Super-S874-010.jpg Super-S1174-020.jpg Super-S862-030.jpg Super-S1162-040.jpg Maybe something like this? foreach($list as $k = $v) { $p = explode('-',$v); $new_list[$p[2]] = $v; } ksort($new_list); reset($new_list); $new_list will have the XXX number as the key of the array element and the name of the file corresponding to the number as it's value. If that won't work, let us know. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Tricky sorting problem
It works! Thanks. On Wednesday, October 30, 2002, at 10:50 AM, @ Edwin wrote: ...and the 2 should be 3 :) - E Marek Kilimajer [EMAIL PROTECTED] wrote: the -5 should be -7 René Fournier wrote: (Well, at least it's tricky for me... :-) I need to sort an array based on three numbers that are situated in the middle of each array element. For example: $list[] is an array, and contains the following [unsorted] elements: Super-S862-030.jpg Super-S1162-040.jpg Super-S874-010.jpg Super-S1174-020.jpg I want to sort $list based on those last three numbers, just before the extension, so that the new sort order is: Super-S874-010.jpg Super-S1174-020.jpg Super-S862-030.jpg Super-S1162-040.jpg I tried writing my own sort function--but can't seem to get anywhere with it: function cmp ($a,$b) { if (substr($a,-5,2) == substr($b,-5,2)) return 0; return (substr($a,-5,2) substr($b,-5,2)) ? -1 : 1 ; } usort($list,cmp); Any ideas? (Thanks.) ...Rene --- René Fournier, [EMAIL PROTECTED] Toll-free +1.888.886.2754 Tel +1.403.291.3601 Fax +1.403.250.5228 www.smartslitters.com SmartSlitters International #33, 1339 - 40th Ave NE Calgary AB T2E 8N6 Canada -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- René Fournier, [EMAIL PROTECTED] Toll-free +1.888.886.2754 Tel +1.403.291.3601 Fax +1.403.250.5228 www.smartslitters.com SmartSlitters International #33, 1339 - 40th Ave NE Calgary AB T2E 8N6 Canada -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php