--- /home/admin/drupal/acidfree/acidfree.module	2005-11-19 07:01:31.000000000 -0600
+++ acidfree.module	2005-11-27 17:15:14.000000000 -0600
@@ -1249,11 +1249,38 @@
     return $base;
 }
 
-function _acidfree_import_dir($parent, $path, $recursive, $node_items) {
+function _acidfree_import_dir($parent, $path, $recursive, $node_items, $sort_order) {
     $dirs = Array();
     // echo "opendir($path)<br>\n";
     $dir = opendir($path);
-    while ($filename = readdir($dir)) {
+    $files = array();
+    if( $dir ){
+      while ($filename = readdir($dir)) {
+        $files[] = $filename;
+      }
+      closedir( $dir );
+    }
+
+    if( ! $sort_order || $sort_order == 0 ){
+      //keep the directory read order.
+      //echo "Keep Directory Read Order<br />\n";
+    } else if( $sort_order == 1 ){
+      //echo "Reverse Directory Read Order<br />\n";
+      $files = array_reverse( $files );
+    } else if( $sort_order == 2 ){
+      //echo "Alpha, Dispay 'A' First<br />\n";
+      array_multisort(array_map( 'strtolower', $files), SORT_DESC, SORT_STRING, $files);
+    } else if( $sort_order == 3 ){
+      //echo "Alpha, Display 'Z' First<br />\n";
+      array_multisort(array_map( 'strtolower', $files), SORT_ASC, SORT_STRING, $files);
+    } else {
+      echo "Oops, Unknown Sort Order Passed<br />\n";
+      //Leave commented out for production other wise we leave ourself
+      //  open to xss problems since we do not escape the value.
+      //echo "Given '$sort_order'<br />\n";
+    }
+
+    foreach ( $files as $filename) {
         if ($filename == '.' || $filename == '..')
             continue;
         $fullpath = "$path/$filename";
@@ -1279,7 +1306,7 @@
             drupal_set_message("failed to import '$filename'", 'error');
         }
     }
-    closedir($dir);
+
     foreach ($dirs as $dir) {
         if (!($cur_album = _acidfree_node_from_file($parent, $dir)))
             continue;
@@ -1290,7 +1317,7 @@
             continue;
         }
         if ($recursive) {
-            _acidfree_import_dir($cur_album, $dir->filepath, $recursive, $node_items);
+            _acidfree_import_dir($cur_album, $dir->filepath, $recursive, $node_items, $sort_order);
         }
     }
 }
@@ -1344,6 +1371,7 @@
                 $node_items->$k = $v;
         }
         $recursive = isset($submitted['recursive']) ? $submitted['recursive'] : true;
+        $sort_order = isset($submitted['sort_order']) ? $submitted['sort_order'] : 0;
         $p = acidfree_get_node_by_id($submitted['parent']);
         switch ($_POST['import']) {
             case t('Import'):
@@ -1365,7 +1393,7 @@
                 break;
         }
         if ($tmpdir) {
-            _acidfree_import_dir($p, $tmpdir, $recursive, $node_items);
+            _acidfree_import_dir($p, $tmpdir, $recursive, $node_items, $sort_order);
             if ($tmpdir != $submitted['path']) {
                 rmdir_rec($tmpdir);
             }
@@ -1389,6 +1417,7 @@
     $import .= form_textfield(t('Path on server'), 'path', '', 50, 256,
             t('This must be the full path to a directory on the server that is readable.'));
     $import .= form_checkbox(t('Include subdirectories'), 'recursive', 1, true);
+    $import .= form_select(t('Import Sort Order'), 'sort_order', '', array( 'Reverse Directory', 'Directory', 'Alpha', 'Reverse Alpha' ), 'The sort order of the import. "Directory" will show the images in the order they were uploaded; "Reverse Directory" will show the last uploaded as the first image (Old default behavior); "Alpha" shows e.g. the images starting with "A" before "B" and "Reverse Alpha" does the opposite. The Alpha sorts are case insensitive. All sorts are per-directory.', 0, 0 );
     $import .= form_button(t('Import'), 'import');
     $output .= form_group(t('Import local'), $import);
     return form($output, 'post', null, array('enctype' => 'multipart/form-data'));
