Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.464
diff -u -p -r1.464 form.inc
--- includes/form.inc	19 May 2010 19:22:24 -0000	1.464
+++ includes/form.inc	20 May 2010 13:34:47 -0000
@@ -2571,13 +2571,26 @@ 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) {
+      // Make sure weightless or weight==0 options get some weight.
+      if (!isset($choice['#weight'])) {
+        $element['#options'][$index]['#weight'] = $count++ / $element_count;
+      }
+      else {
+        $sort = TRUE;
+      }
+    }
+    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.
