moved sortArrayByKey logic into Utils::

This commit is contained in:
Andy Miller
2018-04-09 14:35:44 -06:00
parent 1ec653268d
commit 47037e3f5e
2 changed files with 30 additions and 15 deletions

View File

@@ -371,25 +371,14 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
*
* @param array $input
* @param string $filter
* @param array|int $direction
* @param int $direction
* @param int $sort_flags
*
* @return array
*/
public function sortByKeyFilter($input, $filter, $direction = SORT_ASC)
public function sortByKeyFilter($input, $filter, $direction = SORT_ASC, $sort_flags = SORT_REGULAR)
{
$output = [];
if (!is_array($input) || !$input) {
return $output;
}
foreach ($input as $key => $row) {
$output[$key] = $row[$filter];
}
array_multisort($output, $direction, $input);
return $input;
return Utils::sortArrayByKey($input, $filter, $direction, $sort_flags);
}
/**

View File

@@ -966,6 +966,32 @@ abstract class Utils
return $ordered + $array;
}
/**
* Sort an array by a key value in the array
*
* @param $array
* @param $array_key
* @param int $direction
* @param int $sort_flags
* @return array
*/
public static function sortArrayByKey($array, $array_key, $direction = SORT_DESC, $sort_flags = SORT_REGULAR )
{
$output = [];
if (!is_array($array) || !$array) {
return $output;
}
foreach ($array as $key => $row) {
$output[$key] = $row[$array_key];
}
array_multisort($output, $direction, $sort_flags, $array);
return $array;
}
/**
* Get's path based on a token
*