Jump to content

sorting a scandir array with folders first using usort?


pencapchew

Recommended Posts

i'm making a basic directory listing script... so I use scandir, nothing fancy


[code]$list = scandir($dir);
usort($list,"cmp");[/code]


I'm wanting to use usort to sort the list so that folders come first... and I really have absolutely no idea what i'm doing with usort
I have this:


[code]function cmp($a,$b) {
    if ($a == $b) {
      return 0;
  }
    elseif(is_dir($a)) return -1;
    else return 1;
}[/code]


once again I have no clue what i'm doing. But the above code works - it sorts all the folders first and then files, but in some obscure order. nothing's alphabetic or anything (the original array was), it just seems completely random.. is there anyway I could use usort and still have them alphabetical?..thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.