From eaa05ee2521719b5c02e324bc7335237832eb9d3 Mon Sep 17 00:00:00 2001 From: Djamil Legato Date: Wed, 24 Sep 2014 15:50:21 -0700 Subject: [PATCH] Added new 'sort_by_key' twig filter which sorts an array by the desired key. It also takes in a direction as second argument which can be any of the predefined sorting flags (http://php.net/manual/en/array.constants.php) --- system/src/Grav/Common/TwigExtension.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/system/src/Grav/Common/TwigExtension.php b/system/src/Grav/Common/TwigExtension.php index 9cfff0a83..3fa06aa30 100644 --- a/system/src/Grav/Common/TwigExtension.php +++ b/system/src/Grav/Common/TwigExtension.php @@ -1,5 +1,6 @@ rootUrl($domain) .'/'. $locator->findResource($input, false); } + + /** + * Sorts a collection by key + * + * @param string $input + * @param string $filter + * @param string $direction + * @return string + */ + public function sortByKeyFilter(&$input, $filter, $direction = SORT_ASC) + { + $output = []; + + foreach ($input as $key => $row) { + $output[$key] = $row[$filter]; + } + + array_multisort($output, $direction, $input); + + return $input; + } }