Re: Quick Sort Question
| From: | Jason Lavigne | Date: | Thu, 30 Nov 2000 02:38:00 +0000 |
| Subject: | Re: Quick Sort Question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-27816@lists.php.net to get a copy of this message | ||
Here is some code taken from the PHP site,
http://www.php.net/manual/function.readdir.php
, it shows how to read in a
directory then sort it. You will be left with a sorted array that you can
then output. The sort function, http://www.php.net/manual/function.sort.php
, has some options to make your life easier. Your other two options would be
to do your own code to sort but it would require you to have your data in an
array anyways, so use the function. Your other option would be to do an exec
or system call and do an ls with the flags to sort the way you want, but I
would not recommend (and everyone else here would agree) doing system or
exec calls when there is another solution (see below).
function GetDirArray($sPath)
{
//Load Directory Into Array
$handle=opendir($sPath);
while ($file = readdir($handle))
$retVal[count($retVal)] = $file;
//Clean up and sort
closedir($handle);
sort($retVal);
return $retVal;
}
Jay
----- Original Message -----
From: "Jeff Wilhelm" <jwil7090@postoffice.uri.edu>
To: <php-general@lists.php.net>
Sent: Wednesday, November 29, 2000 8:48 AM
Subject: [PHP] Quick Sort Question
> Good day everyone, I have a quick question dealing with sorting. I use the
> following code to print a list of the MP3s in a given directory, however I
> need some help on sorting the resultant list alphabetically. Currently the
> list is simply displayed (I assume in order of date last modified) but I
was
> hoping to be able to have the entries appear in alphabetical order. I
would
> apprecaite any help you could give me.
>
> <?
> $handle=opendir('.');
> while (false!==($file = readdir($handle))) {
> if (!is_dir($file)) {
> if ($file != "." && $file != ".." &&
> ereg(".mp3",$file)) {
> $filelink = rawurlencode($file);
> echo "<a href='$filelink'>";
> $file = ereg_replace (".mp3","",$file);
> echo "$file</a>";
> echo "<br>";
> }
> }
> }
> closedir($handle);
> ?>
>
> Thanks,
> Jeff
>
>
> --
>
> J.E.F.F. . .W.I.L.H.E.L.M
> Email: jeff@jeffwilhelm.com
> Phone: jeffwilhelm@sprintpcs.com
> Web: http://www.jeffwilhelm.com
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>