Re: usort

From: Date: Sat, 25 Oct 2003 01:03:12 +0000
Subject: Re: usort
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-167511@lists.php.net to get a copy of this message
Curt Zirzow wrote:
* 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
I don't know what the names are. I just know that there might be numbers. :) It still doesn't work. It gives very odd results with the $x_out variables.

« previous php.general (#167511) next »