Index: modules/field/field.form.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/field.form.inc,v
retrieving revision 1.3
diff -u -r1.3 field.form.inc
--- modules/field/field.form.inc	10 Feb 2009 03:16:14 -0000	1.3
+++ modules/field/field.form.inc	11 Feb 2009 07:51:44 -0000
@@ -244,7 +244,7 @@
         $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 -r1.4 field.module
--- modules/field/field.module	10 Feb 2009 03:16:14 -0000	1.4
+++ modules/field/field.module	11 Feb 2009 07:51:45 -0000
@@ -255,7 +255,7 @@
  */
 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 @@
 }
 
 /**
- * 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 @@
 
 /**
  * @} 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 -r1.26 common.test
--- modules/simpletest/tests/common.test	9 Feb 2009 03:29:54 -0000	1.26
+++ modules/simpletest/tests/common.test	11 Feb 2009 07:51:45 -0000
@@ -561,6 +561,90 @@
   }
 }
 
+/**
+ * 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 the default #weight property.
+   */
+  function testDefaultWeight() {
+    // Build an array with '#weight' set for each element.
+    $elements = array(
+      'third' => array(
+        '#weight' => 20,
+      ),
+      'second' => array(
+        '#weight' => 10,
+      ),
+      'first' => array(
+        '#weight' => 0,
+      ),
+    );
+
+    drupal_sort_array($elements);
+    $keys = array_keys($elements);
+    $this->assertTrue($keys[0] == 'first' && $keys[1] == 'second' && $keys[2] == 'third', t('Default parameter weight sorting.'));
+  }
+
+  /**
+   * Test sorting by weight with a custom #weight property.
+   */
+  function testCustomWeight() {
+    // Build an array with '_weight' set for each element.
+    $elements = array(
+      'third' => array(
+        '_weight' => 20,
+      ),
+      'second' => array(
+        '_weight' => 10,
+      ),
+      'first' => array(
+        '_weight' => 0,
+      ),
+    );
+
+    drupal_sort_array($elements, '_weight');
+    $keys = array_keys($elements);
+    $this->assertTrue($keys[0] == 'first' && $keys[1] == 'second' && $keys[2] == 'third', t('Sorting with a custom weight parameter.'));
+  }
+
+  /**
+   * Test sorting by weight with a custom #weight property.
+   */
+  function testChildWeight() {
+    // Build an array with ['weight']['#value'] set for each element.
+    $elements = array(
+      'third' => array(
+        'weight' => array(
+          '#value' => 20,
+        ),
+      ),
+      'second' => array(
+        'weight' => array(
+          '#value' => 10,
+        ),
+      ),
+      'first' => array(
+        'weight' => array(
+          '#value' => 0,
+        ),
+      ),
+    );
+
+    drupal_sort_array($elements, 'weight,#value');
+    $keys = array_keys($elements);
+    $this->assertTrue($keys[0] == 'first' && $keys[1] == 'second' && $keys[2] == 'third', t('Sorting via a child weight parameter.'));
+  }
+}
 
 /**
  * Tests Drupal error and exception handlers.
Index: modules/user/user.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.pages.inc,v
retrieving revision 1.28
diff -u -r1.28 user.pages.inc
--- modules/user/user.pages.inc	3 Feb 2009 17:30:13 -0000	1.28
+++ modules/user/user.pages.inc	11 Feb 2009 07:51:46 -0000
@@ -167,7 +167,7 @@
 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]);
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.865
diff -u -r1.865 common.inc
--- includes/common.inc	9 Feb 2009 03:29:53 -0000	1.865
+++ includes/common.inc	11 Feb 2009 07:51:44 -0000
@@ -2573,7 +2573,7 @@
   $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,30 @@
 }
 
 /**
- * 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.
+*
+* @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 = '#weight') {
+  static $sort_functions = array();
+
+  if (!isset($sort_functions[$key])) {
+    $sort_functions[$key] = create_function('$a,$b', '
+      $keys = explode(",","'.$key.'");
+      foreach ($keys as $key) {
+        $a = is_array($a) && isset($a[$key]) ? $a[$key] : 0;
+        $b = is_array($b) && isset($b[$key]) ? $b[$key] : 0;
+      }
+      if ($a == $b) {
+        return 0;
+      }
+      return ($a < $b) ? -1 : 1;');
   }
-  return ($a_weight < $b_weight) ? -1 : 1;
+
+  return uasort($array, $sort_functions[$key]);
 }
 
 /**
@@ -3436,18 +3451,6 @@
 }
 
 /**
- * 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 +3498,7 @@
   }
   // 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/upload/upload.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/upload/upload.module,v
retrieving revision 1.227
diff -u -r1.227 upload.module
--- modules/upload/upload.module	3 Feb 2009 18:55:32 -0000	1.227
+++ modules/upload/upload.module	11 Feb 2009 07:51:45 -0000
@@ -207,7 +207,7 @@
         $microweight += 0.001;
       }
     }
-    uasort($form_state['values']['files'], 'element_sort');
+    drupal_sort_array($form_state['values']['files'], '#weight');
   }
 }
 
