Index: includes/install.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/install.inc,v
retrieving revision 1.119
diff -u -r1.119 install.inc
--- includes/install.inc	2 Jan 2010 18:56:20 -0000	1.119
+++ includes/install.inc	3 Jan 2010 22:40:20 -0000
@@ -543,35 +543,35 @@
  *   missing.
  */
 function drupal_install_modules($module_list = array(), $disable_modules_installed_hook = FALSE) {
-  $files = system_rebuild_module_data();
+  // Get all module data so we can find dependencies and sort.
+  $module_data = system_rebuild_module_data();
+  // Create an associative array with weights as values.
   $module_list = array_flip(array_values($module_list));
-  do {
-    $moved = FALSE;
-    foreach ($module_list as $module => $weight) {
-      $file = $files[$module];
-      if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
-        foreach ($file->info['dependencies'] as $dependency) {
-          if (!isset($module_list[$dependency])) {
-            if (!isset($files[$dependency])) {
-              // A dependency was not found, abort installation.
-              return FALSE;
-            }
-            elseif (!$files[$dependency]->status) {
-              // Add dependencies to $module_list and install them first.
-              $module_list[$dependency] = $weight - 1;
-              $moved = TRUE;
-            }
-          }
-          elseif ($module_list[$module] < $module_list[$dependency] +1) {
-            $module_list[$module] = $module_list[$dependency] +1;
-            $moved = TRUE;
-          }
-        }
+
+  while (list($module) = each($module_list)) {
+    if (!isset($module_data[$module])) {
+      // This module is not found in the filesystem, abort.
+      return FALSE;
+    }
+    if ($module_data[$module]->status) {
+      // Skip already enabled modules.
+      unset($module_list[$module]);
+      continue;
+    }
+    $module_list[$module] = $module_data[$module]->sort;
+
+    // Add dependencies to the module list.
+    foreach ($module_data[$module]->info['dependencies'] as $dependency) {
+      if (!isset($module_list[$dependency])) {
+        $module_list[$dependency] = 0;
       }
     }
-  } while ($moved);
-  asort($module_list);
+  }
+
+  // Sort the module list by pre-calculated weights.
+  arsort($module_list);
   $module_list = array_keys($module_list);
+
   module_enable($module_list, $disable_modules_installed_hook);
   return TRUE;
 }
