diff --git a/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php b/module/VuFind/src/VuFind/Hierarchy/TreeDataSource/Solr.php index 87e25f6e0c7e63093fff6c3738ba23b88d3d9ca1..65be14bab74075f7bae23424193ae940ef0fa250 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); } /**