RE: [PHP] Sort an array by its values
| From: | Andrew Braund | Date: | Sun, 08 Jul 2001 08:44:10 +0000 |
| Subject: | RE: [PHP] Sort an array by its values | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-56739@lists.php.net to get a copy of this message | ||
I think array_multisort is supposed to do this but I couldn't get it to
work.
Have a look at the usort call in the following routine;
// Reads a directory and puts filenames into a array sorted by date
function GetFiles ($DataPath, &$files, &$nfiles) {
$handle=@opendir($DataPath);
if ($handle) {
$nfiles = 0;
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != ".." ) {
$files[$nfiles][name] = $file;
$files[$nfiles][date] = filemtime($DataPath."\\".$file);
$nfiles++;
}
}
if($nfiles>1){
usort($files,
create_function(
'$a,$b',
'return
$a["date"]==$b["date"]?0:($a["date"]<$b["date"]?1:-1);')
);
}
closedir($handle);
}
}
HTH
Andrew Braund
> -----Original Message-----
> From: Aaron Bennett [mailto:abennett@liquidstudio.com]
> Sent: Sunday, 8 July 2001 17:56
> To: Php-General (E-mail)
> Subject: [PHP] Sort an array by its values
>
>
> Hi All,
> I'm trying to sort an array of objects by the value of one of those
> objects...
> for instance, I'll have an object with 2 properties, "id" and
> "score", where
> id is unique and score is its relevant (and sometimes simelar) score. i've
> tried using sort() and asort() but i can't figure how to base the sorting
> off the value (i.e. score) of the object in the array. Any help?
> --
> Aaron
>