Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.487
diff -u -p -r1.487 form.inc
--- includes/form.inc	30 Aug 2010 17:07:49 -0000	1.487
+++ includes/form.inc	9 Sep 2010 16:26:22 -0000
@@ -2821,13 +2821,29 @@ function form_process_tableselect($eleme
 
   $element['#tree'] = TRUE;
 
-  if (count($element['#options']) > 0) {
+  $element_count = count($element['#options']);
+  if ($element_count > 0) {
     if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
       $element['#default_value'] = array();
     }
 
-    // Sort the options by their #weight if they have a #weight.
-    uasort($element['#options'], 'element_sort');
+    $count = 0;
+    $sort = FALSE;
+    foreach ($element['#options'] as $index => $choice) {
+      // If #weight is not set, add a #weight value that maintains the order of
+      // the #options array elements that lack #weight. The range of the added
+      // #weight values is 0 to less than 1. The elements without #weight will
+      // be sorted as a group between others with #weight < 0 and #weight >= 1.
+      if (isset($choice['#weight'])) {
+        $sort = TRUE;
+      }
+      else {
+        $element['#options'][$index]['#weight'] = $count++ / $element_count;
+      }
+    }
+    if ($sort) {
+      uasort($element['#options'], 'element_sort');
+    }
     // Create a checkbox or radio for each item in #options in such a way that
     // the value of the tableselect element behaves as if it had been of type
     // checkboxes or radios.
Index: modules/node/node.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.test,v
retrieving revision 1.94
diff -u -p -r1.94 node.test
--- modules/node/node.test	30 Aug 2010 00:22:03 -0000	1.94
+++ modules/node/node.test	9 Sep 2010 16:26:22 -0000
@@ -1195,6 +1195,30 @@ class NodeAdminTestCase extends DrupalWe
   }
 
   /**
+   * Tests that the table sorting works on the content admin pages.
+   */
+  function testContentAdminSort() {
+    $this->drupalLogin($this->admin_user);
+    foreach (array('dd', 'aa', 'DD', 'bb', 'cc', 'CC', 'AA', 'BB') as $prefix) {
+      $this->drupalCreateNode(array('title' => $prefix . $this->randomName(6)));
+    }
+
+    // Compare the rendered HTML node list to a query for the nodes ordered by
+    // title to account for possible database-dependent sort order.
+    $nodes_query = db_select('node', 'n')
+      ->fields('n', array('nid'))
+      ->orderBy('title')
+      ->execute()
+      ->fetchCol();
+
+    $this->drupalGet('admin/content', array('query' => array('sort' => 'asc', 'order' => 'Title')));
+    foreach ($this->xpath('//table/tbody/tr/td/div/input/@value') as $input) {
+      $nodes_form[] = $input;
+    }
+    $this->assertEqual($nodes_query, $nodes_form, 'Nodes are sorted in the form the same as they are in the query.');
+  }
+
+  /**
    * Tests content overview with different user permissions.
    *
    * Taxonomy filters are tested separately.
