? includes/database/install.inc
? sites/all/modules/cvs
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.865
diff -u -p -r1.865 common.inc
--- includes/common.inc	9 Feb 2009 03:29:53 -0000	1.865
+++ includes/common.inc	12 Feb 2009 18:21:39 -0000
@@ -2573,7 +2573,7 @@ function drupal_get_js($scope = 'header'
   $embed_suffix = "\n//--><!]]>\n";
 
   // Sort the JavaScript by weight so that it appears in the correct order.
-  uasort($items, 'drupal_sort_weight');
+  drupal_sort_array($items, 'weight');
 
   // Loop through the JavaScript to construct the rendered output.
   foreach ($items as $item) {
@@ -3386,15 +3386,32 @@ function drupal_render_children(&$elemen
 }
 
 /**
- * Function used by uasort to sort structured arrays by weight.
- */
-function element_sort($a, $b) {
-  $a_weight = (is_array($a) && isset($a['#weight'])) ? $a['#weight'] : 0;
-  $b_weight = (is_array($b) && isset($b['#weight'])) ? $b['#weight'] : 0;
-  if ($a_weight == $b_weight) {
-    return 0;
+* Generic sorting mechanism for arrays.
+*
+* @param $array
+*   The array to sort.
+* @param $key
+*   The index to retrieve. Child keys are passed by comma-seperated values ('item,#weight').
+*/
+function drupal_sort_array(&$array, $key) {
+  static $sort_functions = array();
+
+  if (!isset($sort_functions[$key])) {
+    $sort_functions[$key] = create_function('$a,$b', '
+      $keys = explode(",","'.$key.'");
+      $a_weight = $a;
+      $b_weight = $b;
+      foreach ($keys as $key) {
+        $a_weight = is_array($a_weight) && isset($a_weight[$key]) ? $a_weight[$key] : 0;
+        $b_weight = is_array($b_weight) && isset($b_weight[$key]) ? $b_weight[$key] : 0;
+      }
+      if ($a_weight == $b_weight) {
+        return 0;
+      }
+      return ($a_weight < $b_weight) ? -1 : 1;');
   }
-  return ($a_weight < $b_weight) ? -1 : 1;
+
+  return uasort($array, $sort_functions[$key]);
 }
 
 /**
@@ -3436,18 +3453,6 @@ function element_basic_defaults() {
 }
 
 /**
- * Function used by uasort to sort structured arrays by weight, without the property weight prefix.
- */
-function drupal_sort_weight($a, $b) {
-  $a_weight = (is_array($a) && isset($a['weight'])) ? $a['weight'] : 0;
-  $b_weight = (is_array($b) && isset($b['weight'])) ? $b['weight'] : 0;
-  if ($a_weight == $b_weight) {
-    return 0;
-  }
-  return ($a_weight < $b_weight) ? -1 : 1;
-}
-
-/**
  * Check if the key is a property.
  */
 function element_property($key) {
@@ -3495,7 +3500,7 @@ function element_children(&$elements, $s
   }
   // Sort the element if necessary.
   if ($sort && $sortable) {
-    uasort($children, 'element_sort');
+    drupal_sort_array($children, '#weight');
   }
   $elements['#sorted'] = TRUE;
   return array_keys($children);
Index: modules/field/field.form.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/field.form.inc,v
retrieving revision 1.3
diff -u -p -r1.3 field.form.inc
--- modules/field/field.form.inc	10 Feb 2009 03:16:14 -0000	1.3
+++ modules/field/field.form.inc	12 Feb 2009 18:21:41 -0000
@@ -244,7 +244,7 @@ function theme_field_multiple_value_form
         $items[] = &$element[$key];
       }
     }
-    usort($items, '_field_sort_items_value_helper');
+    drupal_sort_array($items, '_weight,#value');
 
     // Add the items as table rows.
     foreach ($items as $key => $item) {
Index: modules/field/field.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/field.module,v
retrieving revision 1.4
diff -u -p -r1.4 field.module
--- modules/field/field.module	10 Feb 2009 03:16:14 -0000	1.4
+++ modules/field/field.module	12 Feb 2009 18:21:41 -0000
@@ -255,7 +255,7 @@ function field_set_empty($field, $items)
  */
 function _field_sort_items($field, $items) {
   if (($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) && isset($items[0]['_weight'])) {
-    usort($items, '_field_sort_items_helper');
+    drupal_sort_array($items, '_weight');
     foreach ($items as $delta => $item) {
       if (is_array($items[$delta])) {
         unset($items[$delta]['_weight']);
@@ -266,31 +266,6 @@ function _field_sort_items($field, $item
 }
 
 /**
- * Sort function for items order.
- * (copied form element_sort(), which acts on #weight keys)
- */
-function _field_sort_items_helper($a, $b) {
-  $a_weight = (is_array($a) && isset($a['_weight'])) ? $a['_weight'] : 0;
-  $b_weight = (is_array($b) && isset($b['_weight'])) ? $b['_weight'] : 0;
-  if ($a_weight == $b_weight) {
-    return 0;
-  }
-  return ($a_weight < $b_weight) ? -1 : 1;
-}
-
-/**
- * Same as above, using ['_weight']['#value']
- */
-function _field_sort_items_value_helper($a, $b) {
-  $a_weight = (is_array($a) && isset($a['_weight']['#value'])) ? $a['_weight']['#value'] : 0;
-  $b_weight = (is_array($b) && isset($b['_weight']['#value'])) ? $b['_weight']['#value'] : 0;
-  if ($a_weight == $b_weight) {
-    return 0;
-  }
-  return ($a_weight < $b_weight) ? -1 : 1;
-}
-
-/**
  * Registry of available build modes.
  * TODO : move into hook_fieldable_info() ?
  */
@@ -597,4 +572,4 @@ function template_preprocess_field(&$var
 
 /**
  * @} End of "defgroup field"
- */
\ No newline at end of file
+ */
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.26
diff -u -p -r1.26 common.test
--- modules/simpletest/tests/common.test	9 Feb 2009 03:29:54 -0000	1.26
+++ modules/simpletest/tests/common.test	12 Feb 2009 18:21:44 -0000
@@ -561,6 +561,77 @@ class DrupalRenderUnitTestCase extends D
   }
 }
 
+/**
+ * Tests for drupal_sort_array().
+ */
+class DrupalSortArrayUnitTestCase extends DrupalWebTestCase {
+  function getInfo() {
+    return array(
+      'name' => t('Array Sorting'),
+      'description' => t('Performs unit tests on drupal_sort_array().'),
+      'group' => t('System'),
+    );
+  }
+
+  /**
+   * Test sorting by weight with a one deep sort.
+   */
+  function testOneDeepKey() {
+    // Build an array with '#weight' set for each element.
+    $elements = array(
+      'fourth' => array(
+        '#weight' => 20,
+      ),
+      'third' => array(
+        '#weight' => 10,
+      ),
+      'second' => array(
+        '#not_weight' => 30,
+      ),
+      'first' => array(
+        '#weight' => -10,
+      ),
+    );
+
+    $sorted_elements = $elements;
+    drupal_sort_array($sorted_elements, '#weight');
+    $this->assertIdentical(array_reverse($elements, TRUE), $sorted_elements, t('One-deep array sorted correctly.'));
+  }
+
+  /**
+   * Test sorting by weight with a two deep sort.
+   */
+  function testTwoDeepKey() {
+    // Build an array with ['weight']['#value'] set for each element.
+    $elements = array(
+      'third' => array(
+        'weight' => array(
+          '#value' => 10,
+        ),
+      ),
+      'second' => array(
+        'weight' => array(
+          '#not_value' => 20,
+        ),
+      ),
+      'first' => array(
+        'weight' => array(
+          '#value' => -10,
+        ),
+      ),
+    );
+
+    // Copy each element in the array by reference to test that
+    // drupal_sort_array() does not modify the elements.
+    $sorted_elements = array();
+    foreach ($elements as $key => &$element) {
+      $sorted_elements[$key] = $element;
+    }
+
+    drupal_sort_array($sorted_elements, 'weight,#value');
+    $this->assertIdentical(array_reverse($elements, TRUE), $sorted_elements, t('Two-deep array sorted correctly.'));
+  }
+}
 
 /**
  * Tests Drupal error and exception handlers.
Index: modules/upload/upload.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/upload/upload.module,v
retrieving revision 1.227
diff -u -p -r1.227 upload.module
--- modules/upload/upload.module	3 Feb 2009 18:55:32 -0000	1.227
+++ modules/upload/upload.module	12 Feb 2009 18:21:54 -0000
@@ -207,7 +207,7 @@ function upload_node_form_submit(&$form,
         $microweight += 0.001;
       }
     }
-    uasort($form_state['values']['files'], 'element_sort');
+    drupal_sort_array($form_state['values']['files'], '#weight');
   }
 }
 
Index: modules/user/user.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.pages.inc,v
retrieving revision 1.28
diff -u -p -r1.28 user.pages.inc
--- modules/user/user.pages.inc	3 Feb 2009 17:30:13 -0000	1.28
+++ modules/user/user.pages.inc	12 Feb 2009 18:21:55 -0000
@@ -167,7 +167,7 @@ function user_view($account) {
 function template_preprocess_user_profile(&$variables) {
   $variables['profile'] = array();
   // Sort sections by weight
-  uasort($variables['account']->content, 'element_sort');
+  drupal_sort_array($variables['account']->content, '#weight');
   // Provide keyed variables so themers can print each section independently.
   foreach (element_children($variables['account']->content) as $key) {
     $variables['profile'][$key] = drupal_render($variables['account']->content[$key]);
