| 6 common.inc | element_sort($a, $b) |
| 7 common.inc | element_sort($a, $b) |
| 8 common.inc | element_sort($a, $b) |
Function used by uasort to sort structured arrays by weight.
2 string references to 'element_sort'
File
- includes/
common.inc, line 3051 - 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;
}
Login or register to post comments
Comments
Usage
So if you've got an array of arrays, each with an '#weight' set on them you can sort them with the code:
<?php// $elements is my array of arrays
uasort($elements, "element_sort");
?>
Need to ensure that ordering is maintained?
Please note that if two elements are regarded as equal by this function, then their order in the sorted array is undefined. In other words, if you leave out #weight, then your array might not even keep its original ordering.
You can read more about this on http://www.php.net/manual/en/array.sorting.php.