6.x common.inc element_sort($a, $b)
7.x common.inc element_sort($a, $b)

Function used by uasort to sort structured arrays by weight.

4 string references to 'element_sort'
comment_view_multiple in modules/comment/comment.module
Construct a drupal_render() style array from an array of loaded comments.
element_children in includes/common.inc
Identifies the children of an element array, optionally sorted by weight.
node_view_multiple in modules/node/node.module
Constructs a drupal_render() style array from an array of loaded nodes.
taxonomy_term_view_multiple in modules/taxonomy/taxonomy.module
Constructs a drupal_render() style array from an array of loaded terms.

File

includes/common.inc, line 6452
Common functions that many Drupal modules will need to reference.

Code

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;
  }
  return ($a_weight < $b_weight) ? -1 : 1;
}

Comments

Garrett Albright’s picture

A usage example for this can be found on the Drupal 6 documentation page for this function (it works identically as in Drupal 7).

Matt-H’s picture

If you wish to sort by the 'weight' array element instead of the render element property '#weight', use the drupal_sort_weight() function.