From 1d2d96080e2304faa71eea5f6cf7b4f5df4178cb Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Tue, 23 Dec 2014 10:28:03 -0500 Subject: [PATCH] Made sort code more functional for readability. --- .../VuFind/Hierarchy/TreeDataSource/Solr.php | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php index 87e25f6e0c7..65be14bab74 100644 --- a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php +++ b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php @@ -182,41 +182,38 @@ class Solr extends AbstractBase htmlspecialchars($title) . '</name></content>'; $xmlNode .= $this->getChildren($current->getUniqueID(), $count); $xmlNode .= '</item>'; - array_push($xml, array((isset($sequence) ? $sequence : 0), $xmlNode)); - } - if ($sorting) { - $this->sortNodes($xml, 0); + // If we're in sorting mode, we need to create key-value arrays; + // otherwise, we can just collect flat strings. + $xml[] = $sorting + ? array(isset($sequence) ? $sequence : 0, $xmlNode) + : $xmlNode; } - $xmlReturnString = ''; - foreach ($xml as $node) { - $xmlReturnString .= $node[1]; - } - return $xmlReturnString; + // Assemble the XML, sorting it first if necessary: + return implode('', $sorting ? $this->sortNodes($xml) : $xml); } /** * Sort Nodes * - * @param array &$array The Array to Sort - * @param string $key The key to sort on + * @param array $array The array to sort * * @return void */ - protected function sortNodes(&$array, $key) + protected function sortNodes($array) { - $sorter=array(); - $ret=array(); - reset($array); - foreach ($array as $ii => $va) { - $sorter[$ii]=$va[$key]; - } - asort($sorter); - foreach ($sorter as $ii => $va) { - $ret[$ii]=$array[$ii]; - } - $array=$ret; + // Sort arrays based on first element + $sorter = function ($a, $b) { + return strcmp($a[0], $b[0]); + }; + usort($array, $sorter); + + // Collapse array to remove sort values + $mapper = function ($i) { + return $i[1]; + }; + return array_map($mapper, $array); } /** -- GitLab