Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12060

Re: sorting readdir output?

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: sorting readdir output?
Date 2013-12-01 23:41 +0000
Organization A noiseless patient Spider
Message-ID <l7ghf7$3ck$1@dont-email.me> (permalink)
References <1g1k8whk9o5qm.19jj117exjuzm.dlg@40tude.net>

Show all headers | View raw


On Sun, 01 Dec 2013 11:39:20 -0500, richard wrote:

> <?php
> 
> if ($handle = opendir('../audio/1960/')) {
>     echo "Directory handle: $handle\n";
>     echo "Entries:\n";
> 
>     /* This is the correct way to loop over the directory. */
>     while (false !== ($entry = readdir($handle))) {
>         echo "$entry\n<br>";

Make sure here that you use the form of break tag that matches your 
doctype.

>     }
> 
>     closedir($handle);
> }
> ?>
> 
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?
> 
> http://us1.php.net/readdir

What array? At the moment you're working with a directory handle, from 
which you're reading one filename at a time. They come in whatever order 
the underlying os code delivers them.

If you want them sorted, read them in to an array and sort it, and then 
output the array in order, but at the moment, in your code, there is no 
array.

Without testing, the following might work:

<?php

$files = array();

if ( $handle = opendir ( '../audio/1960/' ) ) while ( false !== ( $entry 
= readdir( $handle ) ) ) $files[] = $entry;

sort( $files );

foreach ( $files as $file ) echo "{$file}<br>\n";

?>

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

sorting readdir output? richard <noreply@example.com> - 2013-12-01 11:39 -0500
  Re: sorting readdir output? Norman Peelman <npeelman@cfl.rr.com> - 2013-12-01 11:56 -0500
    Re: sorting readdir output? richard <noreply@example.com> - 2013-12-01 12:13 -0500
  Re: sorting readdir output? Luuk <luuk@invalid.lan> - 2013-12-01 18:32 +0100
    Re: sorting readdir output? Luuk <luuk@invalid.lan> - 2013-12-01 18:35 +0100
    Re: sorting readdir output? richard <noreply@example.com> - 2013-12-01 13:10 -0500
      Re: sorting readdir output? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2013-12-01 13:19 -0500
  Re: sorting readdir output? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2013-12-01 12:59 -0500
    Re: sorting readdir output? "James Harris" <james.harris.1@gmail.com> - 2013-12-01 18:10 +0000
      Re: sorting readdir output? richard <noreply@example.com> - 2013-12-01 13:19 -0500
  Re: sorting readdir output? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-01 23:41 +0000
  Re: sorting readdir output? Richard Yates <richard@yatesguitar.com> - 2013-12-02 10:54 -0800
    Re: sorting readdir output? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-02 20:24 +0000
      Re: sorting readdir output? Richard Yates <richard@yatesguitar.com> - 2013-12-02 13:02 -0800
        Re: sorting readdir output? Christoph Michael Becker <cmbecker69@arcor.de> - 2013-12-02 22:33 +0100
      Re: sorting readdir output? Tim Streater <timstreater@greenbee.net> - 2013-12-02 22:20 +0000
        Re: sorting readdir output? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-02 23:54 +0000

csiph-web