diff --git a/modules/simpletest/tests/module_test.module b/modules/simpletest/tests/module_test.module
index facac7cfafa37d1cd7eab3aee2f43d13712950ae..6829026a275239adacfbdf8f5940f36a31c0724b 100644
--- a/modules/simpletest/tests/module_test.module
+++ b/modules/simpletest/tests/module_test.module
@@ -50,3 +50,10 @@ function module_test_hook_info() {
   );
   return $hooks;
 }
+
+/**
+ * Implements hook_modules_enabled().
+ */
+function module_test_modules_enabled($modules) {
+  variable_set('test_module_enable_order', $modules);
+}
diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc
index 736fa5e7054be8ee8e8c9fa7a9a79e82bd599d67..74596927608dd08cc6bca988e131c2044216e25e 100644
--- a/modules/system/system.admin.inc
+++ b/modules/system/system.admin.inc
@@ -1186,7 +1186,7 @@ function system_modules_submit($form, &$form_state) {
   unset($form_state['storage']);
 
   // Reverse the 'enable' list, to order dependencies before dependents.
-  rsort($actions['enable']);
+  krsort($actions['enable']);
 
   // Installs, enables, and disables modules.
   module_enable($actions['enable'], FALSE);
diff --git a/modules/system/system.test b/modules/system/system.test
index 44768352db0c1564ae545347d24076da1e7da21a..bc5f5593fc61e2e3f1515df0d171285c5ea7bebe 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -318,6 +318,35 @@ class ModuleDependencyTestCase extends ModuleTestCase {
     $this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
     $this->assertModules(array('taxonomy', 'forum'), TRUE);
   }
+
+  /**
+   * Tests that module dependencies are enabled in the correct order via the
+   * UI. Dependencies should be enabled before their dependents.
+   */
+  function testModuleEnableOrder() {
+    module_enable(array('module_test'), FALSE);
+    $this->resetAll();
+    $this->assertModules(array('module_test'), TRUE);
+    variable_set('dependency_test', 'dependency');
+    // module_test creates a dependency chain: forum depends on poll, which
+    // depends on php. The correct enable order is, php, poll, forum.
+    $expected_order = array('php', 'poll', 'forum');
+
+    // Enable the modules through the UI, verifying that the dependency chain
+    // is correct.
+    $edit = array();
+    $edit['modules[Core][forum][enable]'] = 'forum';
+    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
+    $this->assertModules(array('forum'), FALSE);
+    $this->assertText(t('You must enable the Poll, PHP filter modules to install Forum.'), t('Dependency chain created.'));
+    $edit['modules[Core][poll][enable]'] = 'poll';
+    $edit['modules[Core][php][enable]'] = 'php';
+    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
+    $this->assertModules(array('forum', 'poll', 'php'), TRUE);
+
+    // Check the actual order which is saved by module_test_modules_enabled().
+    $this->assertIdentical(variable_get('test_module_enable_order', FALSE), $expected_order, t('Modules enabled in the correct order.'));
+  }
 }
 
 /**