=== modified file 'database/database.mysql'
--- database/database.mysql	
+++ database/database.mysql	
@@ -669,7 +669,9 @@ CREATE TABLE system (
   throttle tinyint(1) DEFAULT '0' NOT NULL,
   bootstrap int(2) NOT NULL default '0',
   schema_version smallint(2) unsigned NOT NULL default 0,
-  PRIMARY KEY (filename)
+  depth int(2) NOT NULL default '0',
+  PRIMARY KEY (filename),
+  KEY (depth)
 ) TYPE=MyISAM;
 
 --
=== modified file 'database/database.pgsql'
--- database/database.pgsql	
+++ database/database.pgsql	
@@ -664,8 +664,10 @@ CREATE TABLE system (
   throttle smallint NOT NULL default '0',
   bootstrap integer NOT NULL default '0',
   schema_version smallint NOT NULL default 0,
+  depth smallint NOT NULL default 0,
   PRIMARY KEY (filename)
 );
+CREATE INDEX system_depth_idx ON system(depth);
 
 --
 -- Table structure for term_data
=== modified file 'database/updates.inc'
--- database/updates.inc	
+++ database/updates.inc	
@@ -1335,9 +1335,21 @@ function system_update_164() {
 }
 
 function system_update_165() {
-  $cron_last = max(variable_get('drupal_cron_last'), variable_get('ping_cron_last'));
-  variable_set('cron_last', $cron_last);
-  variable_del('drupal_cron_last');
-  variable_del('ping_cron_last');
-  return array();
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      db_add_column($ret, 'system', 'depth', 'smallint', array('not null' => TRUE, 'default' => 0));
+      $ret[] = update_sql('CREATE INDEX {system}_depth_idx ON {system} (depth)');
+      break;
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("ALTER TABLE {system} ADD depth tinyint(3) unsigned default '0' NOT NULL, ADD KEY (depth)");
+      break;
+  }
+  $result = db_query("SELECT filename FROM {system} WHERE type = 'module'");
+  while ($module = db_fetch_object($result)) {
+    db_query("UPDATE {system} SET depth = %d WHERE filename = '%s'", strlen(preg_replace('|[^/]|', '', $module->filename)), $module->filename);
+  }
+
+  return $ret;
 }
=== modified file 'includes/module.inc'
--- includes/module.inc	
+++ includes/module.inc	
@@ -50,10 +50,10 @@ function module_list($refresh = FALSE, $
   if (!$list) {
     $list = array('filter' => 'filter', 'system' => 'system', 'user' => 'user', 'watchdog' => 'watchdog');
     if ($bootstrap) {
-      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1");
+      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1 ORDER BY depth ASC, filename ASC");
     }
     else {
-      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1");
+      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 ORDER BY depth ASC, filename ASC");
     }
     while ($module = db_fetch_object($result)) {
       if (file_exists($module->filename)) {
=== modified file 'modules/system.module'
--- modules/system.module	
+++ modules/system.module	
@@ -923,7 +923,7 @@ function system_modules() {
     }
     else {
       // This is a new module.
-      db_query("INSERT INTO {system} (name, description, type, filename, status, throttle, bootstrap) VALUES ('%s', '%s', '%s', '%s', %d, %d, %d)", $file->name, $file->description, 'module', $file->filename, $file->status, $file->throttle, $bootstrap);
+      db_query("INSERT INTO {system} (name, description, type, filename, status, throttle, bootstrap, depth) VALUES ('%s', '%s', '%s', '%s', %d, %d, %d, %d)", $file->name, $file->description, 'module', $file->filename, $file->status, $file->throttle, $bootstrap, strlen(preg_replace('|[^/]|', '', $file->filename)));
     }
   }
 
