Page 1 of 1

Sorting files by file name... not working

Posted: Tue Jun 13, 2006 1:16 pm
by Luke
I am setting up a file management system. I have a piece of code that allows user to sort by a number of the file's attributes. Here is the code:

Code: Select all

if ($open = @opendir($GLOBALS['home_directory']  . $user->user_dir.$path))
  {
   for($i=0;($file = readdir($open)) != FALSE;$i++)
    if (is_file($GLOBALS['home_directory']  . $user->user_dir.$path.$file) && !is_hidden_file($GLOBALS['home_directory']  . $user->user_dir.$path.$file))
    {
     $icon = get_icon($file);
     $filesize = filesize($GLOBALS['home_directory']  . $user->user_dir.$path.$file);
     $permissions = decoct(fileperms($GLOBALS['home_directory']  . $user->user_dir.$path.$file)%01000);
     $modified = filemtime($GLOBALS['home_directory']  . $user->user_dir.$path.$file);
     $extension = "";
     $files[$i] = array(
                         "icon"        => $icon,
                         "filename"    => $file,
                         "filesize"    => $filesize,
                         "permissions" => $permissions,
                         "modified"    => $modified,
                         "extension"   => $extension,
                       );
    }
   closedir($open);

   if (isset($files))
   {
    usort($files, "compare_filedata");
    reset($files);
   }
  }

function compare_filedata ($a, $b)	## Compare filedata (used to sort)
{
 if (is_int($a[$_GET['sortby']]) && is_int($b[$_GET['sortby']]))
 {
  if ($a[$_GET['sortby']]==$b[$_GET['sortby']]) return 0;

  if ($_GET['order'] == "asc")
  {
   if ($a[$_GET['sortby']] > $b[$_GET['sortby']]) return 1;
   else return -1;
  }
  else if ($_GET['order'] == "desc")
  {
   if ($a[$_GET['sortby']] < $b[$_GET['sortby']]) return 1;
   else return -1;
  }
 }

 else if (is_string($a[$_GET['sortby']]) && is_string($b[$_GET['sortby']]) && $_GET['order'] == "asc")
  return strcmp($a[$_GET['sortby']], $b[$_GET['sortby']]);
 else if (is_string($a[$_GET['sortby']]) && is_string($b[$_GET['sortby']]) && $_GET['order'] == "desc")
  return -strcmp($a[$_GET['sortby']], $b[$_GET['sortby']]);
}
When I try to sort by file name with this piece of code, I get this:
01 - Van Halen - Eruption.mp3
24261446.jpg
30763154.jpg
30763250.jpg
Hone_stop_career_centers.gif
banbnbnner.gif
index.php
nike.mov
notes.txt
sign_off.doc
something1.mno
I can't figure out how it comes up with that sort... it makes no sense! anybody?

Posted: Tue Jun 13, 2006 1:36 pm
by Luke
disregard this... I figured it out... it needed to be strtolower()'d before comparing. Thanks anyway~!