Sort an array but ...

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
MysticFist
Forum Newbie
Posts: 1
Joined: Sun Jul 23, 2006 12:29 pm

Sort an array but ...

Post by MysticFist »

Hi,

I started a few weeks ago to use php because i want to make a gallery for the pictures of my daughter.
I succeeded in creating a php gallery that do what i want except that the menu is giving me a little problem.
In fact my menu is reading the directories that are in my current directory and use these names to make the menu.
The name of my directories are like MM-YYYY but of course this is not sorting correctly.
It gives me something like this:

01-2006
02-2006
...
10-2005
11-2005
12-2005

And of course i want it to be the following:

...
10-2005
11-2005
12-2005
01-2006
02-2006

Some ppl will say to put the year at the first position followed by the months but as i am dutch we are used to place the months before the year.
Can someone tell me how i could sort my array containing all the directorynames to fit what i want?

This is the code without placing it in an array and it gives me the first result.
I tried several solutions by placing it in an array but then i don't know how to start to fix my problem.
Thanks in advance for any help.

Code: Select all

$dirname = ".";
$dh = opendir($dirname);
while ( gettype($file = readdir($dh)) != "boolean" )
{
  if ( is_dir( "$dirname/$file" ) && ( $file != "." ) && ( $file != ".." ) )
    { 
      print "      <a href=\"cg_gallery1.php?dir=$file\">$file</a>\n      <br/>\n";
    }
}
closedir ($dh);

PS: I need those directories because my gallery is using the name of the directory to read all the images in the directory to make my gallery.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Create a function that can tell if one month-year combination is greater than another month-year combination. Once you have that, use usort()
Post Reply