usort
| From: | Shmuel | Date: | Fri, 24 Oct 2003 23:51:35 +0000 |
| Subject: | usort | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-167504@lists.php.net to get a copy of this message | ||
Hi,
I'm trying sort an array. It has entries like this:
file_1.ext
file_2.ext
file_3.ext
file_10.ext
file_40.ext
and so on.
I want to sort them alphabetically first and then by the numbers.
If I sort them normally I get them like this:
file_1.ext
file_10.ext
file_2.ext
file_3.ext
file_40.ext
What I'm thinking of is like this:
function mycmp($a, $b) {
if ($a == $b) {
return 0;} $a_n = preg_match("/\D+(\d+)\D+/", $a, $a_out); $b_n = preg_match("/\D+(\d+)\D+/", $b, $b_out); return ($a_n[1] < $b_n[1]) ? -1 : 1; } But it doesn't work the way it should. Any suggestions? So this is a the callback-function for the usort funtion. Shmuel.