1 file.module _file_sort_array_by_weight(&$a)

Sorts an array by weight.

Helper function to sort an array by the value of each item's 'weight' key, while preserving relative order of items that have equal weight.

File

core/modules/file/file.module, line 2622
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function _file_sort_array_by_weight(&$a) {
  if (!empty($a)) {
    $i = 0;
    foreach ($a as $key => $item) {
      if (!isset($a[$key]['weight'])) {
        $a[$key]['weight'] = 0;
      }
      $original_weight[$key] = $a[$key]['weight'];
      $a[$key]['weight'] += $i / 1000;
      $i++;
    }
    backdrop_sort($a, array('weight' => SORT_NUMERIC));
    foreach ($a as $key => $item) {
      $a[$key]['weight'] = $original_weight[$key];
    }
  }
}