Sorting number reversed problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Sorting number reversed problem...

Post by Peuplarchie »

Good day top you all,
I'm working on a script that read a folder a list directory.
The files in that folder are named by number, like 0 to 200.

here how it would look like :

003
002
001
010
...

How can I make my list in complete reverse order like :

010
...
003
002
001


here is my code :

Code: Select all

 
function listFilesInDir($start_dir)
 
        {
        
        /*
        returns an array of files in $start_dir (not recursive)
        */
                
        $files = array();
        $dir = opendir($start_dir);
$count =0;
        while(($myfile = readdir($dir)) !== false)
                {
                if($myfile != '.' && $myfile != '..' && !is_file($myfile) && $myfile != 'resource.frk' && !eregi('^Icon',$myfile) )
                        {
                        $count = $count +1; 
                        $files[] = $myfile;
                        }
                }
        closedir($dir);
        rsort($files);
        return $files;
        
 
        }
 
Thanks !
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Sorting number reversed problem...

Post by Burrito »

what you have should do it.

rsort() will sort the the array in descending order. Keep in mind though that strings sort differently than integers.

ex: 10.php would be before 9.php.
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Re: Sorting number reversed problem...

Post by Peuplarchie »

how can I fix this...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Sorting number reversed problem...

Post by Burrito »

my suggestion would be to split the filename from its extension and run it through intval() to parse the names as ints then use that as the key for the array.

you can then use krsort() to sort it by key and extract the values from the array from there.
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Re: Sorting number reversed problem...

Post by Peuplarchie »

sounds great, looking around to see how could I code that....
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Re: Sorting number reversed problem...

Post by Peuplarchie »

Solved !

Code: Select all

 
natsort($files);
$files = array_reverse($files);  
 
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Sorting number reversed problem...

Post by Burrito »

very nice. didn't know about natsort()...I like it :)
Post Reply