Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.403
diff -u -9 -p -r1.403 form.inc
--- includes/form.inc	22 Nov 2009 07:32:47 -0000	1.403
+++ includes/form.inc	22 Nov 2009 13:01:49 -0000
@@ -1082,21 +1082,18 @@ function form_builder($form_id, $element
   if (isset($element['#process']) && !$element['#processed']) {
     foreach ($element['#process'] as $process) {
       if (function_exists($process)) {
         $element = $process($element, $form_state, $form_state['complete form']);
       }
     }
     $element['#processed'] = TRUE;
   }
 
-  // We start off assuming all form elements are in the correct order.
-  $element['#sorted'] = TRUE;
-
   // Recurse through all child elements.
   $count = 0;
   foreach (element_children($element) as $key) {
     // Don't squash an existing tree value.
     if (!isset($element[$key]['#tree'])) {
       $element[$key]['#tree'] = $element['#tree'];
     }
 
     // Deny access to child elements if parent is denied.
@@ -1111,29 +1108,34 @@ function form_builder($form_id, $element
       $element[$key]['#parents'] = $element[$key]['#tree'] && $element['#tree'] ? array_merge($element['#parents'], array($key)) : array($key);
     }
     // Ensure #array_parents follows the actual form structure.
     $array_parents = isset($element['#array_parents']) ? $element['#array_parents'] : array();
     $array_parents[] = $key;
     $element[$key]['#array_parents'] = $array_parents;
 
     // Assign a decimal placeholder weight to preserve original array order.
     if (!isset($element[$key]['#weight'])) {
-      $element[$key]['#weight'] = $count/1000;
+      $element[$key]['#weight'] = $count / 1000;
     }
-    else {
+    elseif (empty($element['#sorted'])) {
       // If one of the child elements has a weight then we will need to sort
       // later.
-      unset($element['#sorted']);
+      $element['#sorted'] = FALSE;
     }
     $element[$key] = form_builder($form_id, $element[$key], $form_state);
     $count++;
   }
 
+  // If we did not set $element['#sorted'] to FALSE, the array was already sorted.
+  if (!isset($element['#sorted'])) {
+    $element['#sorted'] = TRUE;
+  }
+
   // The #after_build flag allows any piece of a form to be altered
   // after normal input parsing has been completed.
   if (isset($element['#after_build']) && !isset($element['#after_build_done'])) {
     foreach ($element['#after_build'] as $function) {
       $element = $function($element, $form_state);
       $element['#after_build_done'] = TRUE;
     }
   }
 
@@ -2468,29 +2470,32 @@ function form_process_fieldset(&$element
  *
  * @param $element
  *   An associative array containing the properties and children of the
  *   fieldset.
  * @return
  *   The modified element with all group members.
  */
 function form_pre_render_fieldset($element) {
   if (!empty($element['#group_members'])) {
-    // Add the group members to this fieldset for rendering purposes only.
-    foreach (element_children($element['#group_members']) as $key) {
-      // This was set in form_process_fieldset so that fieldsets which are
-      // added to groups are not rendered at their original location.
-      // drupal_render_children() will set this back to TRUE.
-      unset($element['#group_members'][$key]['#printed']);
-      $element[] = &$element['#group_members'][$key];
-    }
+    $children = element_children($element['#group_members']);
+    if ($children) {
+      // Add the group members to this fieldset for rendering purposes only.
+      foreach ($children as $key) {
+        // This was set in form_process_fieldset so that fieldsets which are
+        // added to groups are not rendered at their original location.
+        // drupal_render_children() will set this back to TRUE.
+        unset($element['#group_members'][$key]['#printed']);
+        $element[] = &$element['#group_members'][$key];
+      }
 
-    // Resort the element's children after the group members have been added.
-    $element['#sorted'] = FALSE;
+      // Resort the element's children after the group members have been added.
+      $element['#sorted'] = FALSE;
+    }
   }
 
   return $element;
 }
 
 /**
  * Creates a group formatted as vertical tabs.
  *
  * @param $element
Index: modules/system/system.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.admin.inc,v
retrieving revision 1.226
diff -u -9 -p -r1.226 system.admin.inc
--- modules/system/system.admin.inc	21 Nov 2009 17:01:31 -0000	1.226
+++ modules/system/system.admin.inc	22 Nov 2009 13:01:50 -0000
@@ -769,18 +769,19 @@ function system_modules($form, $form_sta
     }
     $form['modules'][$module->info['package']][$filename] = _system_modules_build_row($module->info, $extra);
   }
   // Add basic information to the fieldsets.
   foreach (element_children($form['modules']) as $package) {
     $form['modules'][$package] += array(
       '#type' => 'fieldset',
       '#title' => t($package),
       '#collapsible' => TRUE,
+      '#sorted' => TRUE,
       '#theme' => 'system_modules_fieldset',
       '#header' => array(
         array('data' => t('Enabled'), 'class' => array('checkbox')),
         t('Name'),
         t('Version'),
         t('Description'),
         array('data' => t('Operations'), 'colspan' => 3),
       ),
     );
