Skip to main content
Inspiring
April 14, 2008
Question

Sorting an array of filenames by filetype?

  • April 14, 2008
  • 9 replies
  • 571 views
I have an array of filenames read from a directory list. I can parse the
file type from each filename using a function. How would I then sort the
array by their respective file type, so that all PDF files sort together in
the array, for example? Or would it be easier to just loop through the
array by filetype, printing each matching array value?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


This topic has been closed for replies.

9 replies

Inspiring
April 14, 2008
That's exactly the code I am using except without the multisort. It should
be an easy retrofit!

Thanks, Joe!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns9A80799FF4649makowiecatnycapdotrE@216.104.212.96...
> On 14 Apr 2008 in macromedia.dreamweaver.appdev, Joe Makowiec wrote:
>
>> On 14 Apr 2008 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>>
>>> I have an array of filenames read from a directory list. I can
>>> parse the file type from each filename using a function. How would
>>> I then sort the array by their respective file type, so that all
>>> PDF files sort together in the array, for example? Or would it be
>>> easier to just loop through the array by filetype, printing each
>>> matching array value?
>>
>> http://www.php.net/manual/en/ref.array.php
>>
>> There are a bunch of sorting functions there. array_multisort and
>> uasort both look interesting. Got code?
>
> Dropin for a directory listing in a <ul>:
>
> <pre>
> <?php
> $files2 = array();
> if ($handle = opendir('.')) {
> echo "Input: .\n";
> echo "Directory handle: $handle\n";
> /* This is the correct way to loop over the directory. */
> while (false !== ($file = readdir($handle))) {
> $files[] = $file;
> if (substr($file, 0, 1) != '.') {
> $separator = split('\.', $file);
> $files2[] = array('filename' => $separator[0], 'ext' =>
> $separator[1]);
> }
> echo "$file\n";
> }
> closedir($handle);
>
> // print_r($files);
> // print_r($files2);
>
> foreach ($files2 as $key => $row) {
> $filename[$key] = $row['filename'];
> $ext[$key] = $row['ext'];
> }
>
> array_multisort($ext, SORT_ASC, $filename, SORT_DESC, $files2);
>
> // print_r($files2);
>
> reset($files2);
>
> }
> ?>
> </pre>
> <ul>
> <?php
> foreach ($files2 as $myfile) {
> echo "\t<li>";
> echo $myfile['filename'];
> echo (empty($myfile['ext'])) ? '' : '.' . $myfile['ext'];
> echo "</li>\n";
> }
> ?>
> </ul>
>
> Code's probably uglier than it needs to be, but it works.
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/contact.php

Inspiring
April 14, 2008
On 14 Apr 2008 in macromedia.dreamweaver.appdev, Joe Makowiec wrote:

> On 14 Apr 2008 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> I have an array of filenames read from a directory list. I can
>> parse the file type from each filename using a function. How would
>> I then sort the array by their respective file type, so that all
>> PDF files sort together in the array, for example? Or would it be
>> easier to just loop through the array by filetype, printing each
>> matching array value?
>
> http://www.php.net/manual/en/ref.array.php
>
> There are a bunch of sorting functions there. array_multisort and
> uasort both look interesting. Got code?

Dropin for a directory listing in a <ul>:

<pre>
<?php
$files2 = array();
if ($handle = opendir('.')) {
echo "Input: .\n";
echo "Directory handle: $handle\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$files[] = $file;
if (substr($file, 0, 1) != '.') {
$separator = split('\.', $file);
$files2[] = array('filename' => $separator[0], 'ext' =>
$separator[1]);
}
echo "$file\n";
}
closedir($handle);

// print_r($files);
// print_r($files2);

foreach ($files2 as $key => $row) {
$filename[$key] = $row['filename'];
$ext[$key] = $row['ext'];
}

array_multisort($ext, SORT_ASC, $filename, SORT_DESC, $files2);

// print_r($files2);

reset($files2);

}
?>
</pre>
<ul>
<?php
foreach ($files2 as $myfile) {
echo "\t<li>";
echo $myfile['filename'];
echo (empty($myfile['ext'])) ? '' : '.' . $myfile['ext'];
echo "</li>\n";
}
?>
</ul>

Code's probably uglier than it needs to be, but it works.

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/contact.php
Inspiring
April 14, 2008
That's right! I remember seeing that in the manual. Thanks, I'll look into
it!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Michael Fesser" <netizen@gmx.de> wrote in message
news:tjt604hqjd9cccde753mi5se4fu6rpdro9@4ax.com...
> .oO(Murray *ACE*)
>
>>I have an array of filenames read from a directory list. I can parse the
>>file type from each filename using a function. How would I then sort the
>>array by their respective file type, so that all PDF files sort together
>>in
>>the array, for example? Or would it be easier to just loop through the
>>array by filetype, printing each matching array value?
>
> You can always use usort() with your own compare function. But if you
> need a more complicated result, for example sorted by both type and
> name, then have a look at array_multisort(). It looks a bit complicated
> at first, but is very powerful. You would just have to split your data
> into "columns", i.e. you need one array with all the filenames, a second
> array with all the types and so on. These arrays can then be passed to
> array_multisort(). The manual shows an example for that.
>
> HTH
> Micha

Inspiring
April 14, 2008
.oO(Murray *ACE*)

>I have an array of filenames read from a directory list. I can parse the
>file type from each filename using a function. How would I then sort the
>array by their respective file type, so that all PDF files sort together in
>the array, for example? Or would it be easier to just loop through the
>array by filetype, printing each matching array value?

You can always use usort() with your own compare function. But if you
need a more complicated result, for example sorted by both type and
name, then have a look at array_multisort(). It looks a bit complicated
at first, but is very powerful. You would just have to split your data
into "columns", i.e. you need one array with all the filenames, a second
array with all the types and so on. These arrays can then be passed to
array_multisort(). The manual shows an example for that.

HTH
Micha
Inspiring
April 14, 2008
On 14 Apr 2008 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:

> I have an array of filenames read from a directory list. I can
> parse the file type from each filename using a function. How would
> I then sort the array by their respective file type, so that all PDF
> files sort together in the array, for example? Or would it be
> easier to just loop through the array by filetype, printing each
> matching array value?

http://www.php.net/manual/en/ref.array.php

There are a bunch of sorting functions there. array_multisort and uasort
both look interesting. Got code?

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/contact.php
Inspiring
April 14, 2008
I suppose I could use SQL to group the results, but I don't know how to do
the string manipulations in SQL....

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Massimo Foti" <massimo@massimocorner.com> wrote in message
news:ftvnls$hl0$1@forums.macromedia.com...
> "Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
> news:ftvn68$h4m$1@forums.macromedia.com...
>> Right - grouping. It's PHP.
>
> Sorry, my PHP is really too rusty to give you a decent answer
>
> Massimo
>

Inspiring
April 14, 2008
"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:ftvn68$h4m$1@forums.macromedia.com...
> Right - grouping. It's PHP.

Sorry, my PHP is really too rusty to give you a decent answer

Massimo


Inspiring
April 14, 2008
Right - grouping. It's PHP.

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Massimo Foti" <massimo@massimocorner.com> wrote in message
news:ftvl05$ep3$1@forums.macromedia.com...
> "Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
> news:ftvipf$cjr$1@forums.macromedia.com...
>>I have an array of filenames read from a directory list. I can parse the
>>file type from each filename using a function. How would I then sort the
>>array by their respective file type, so that all PDF files sort together
>>in the array, for example? Or would it be easier to just loop through the
>>array by filetype, printing each matching array value?
>
> Which server-side language you are using?
>
> BTW Seems you are loking for "grouping", not "sorting"
>
> Massimo
>

Inspiring
April 14, 2008
"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:ftvipf$cjr$1@forums.macromedia.com...
>I have an array of filenames read from a directory list. I can parse the
>file type from each filename using a function. How would I then sort the
>array by their respective file type, so that all PDF files sort together in
>the array, for example? Or would it be easier to just loop through the
>array by filetype, printing each matching array value?

Which server-side language you are using?

BTW Seems you are loking for "grouping", not "sorting"

Massimo