function form_field_sort_compare_by_weight

4.x forms.inc form_field_sort_compare_by_weight($element_a, $element_b)

This function is meant to be used by the uasort command, to help re-order a form array based on each element's weight value. If weight is blank, it is assumed to be 0 (zero).

Answers the question "is A LESS THAN b?" with: -1 == true. 0 == they are equal. 1 = no, b is less than a.

File

includes/forms.inc, line 89

Code

function form_field_sort_compare_by_weight($element_a, $element_b) {

  if (!is_array($element_a) || !is_array($element_b)) {
    return 0;
  }

  $weight_a = $element_a ["weight"] * 1;
  $weight_b = $element_b ["weight"] * 1;

  if ($weight_a == $weight_b) {
    return -1;
  }

  return ($weight_a < $weight_b) ? -1 : 1;

}