Re: usort

From: Date: Sat, 25 Oct 2003 00:14:03 +0000
Subject: Re: usort
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-167507@lists.php.net to get a copy of this message
* Thus wrote Shmuel (samug@vectorstar.net): > file_1.ext > file_2.ext > file_3.ext > file_10.ext > file_40.ext > > 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; you need to use the $x_out variables: return ($a_out[1] < $b_out[1]) ? -1 : 1; if all the names are prefixed with 'file_' it might be a bit more efficiant if you do something other than preg_match(): $a_n = (int) substr($a, 5); // (int) forces 10.ext -> 10 $b_n = (int) substr($b, 5); return ($a_n < $b_n) ? -1 : 1; Curt -- "My PHP key is worn out" PHP List stats since 1997: http://zirzow.dyndns.org/html/mlists/

« previous php.general (#167507) next »