/** * Sort by QuickSort technique * FROM: http://martinjansen.com/projects/Quicksort/ * * @param array $array Input array * @param int $firstElement Element to start the sort * @param int $lastElement Element where the sort should stop * @return bool True if sort was ok, false if not. * @access public */ static function QuickSort(&$array, $firstElement = null, $lastElement = null) { if (!is_array($array)) { return false; } if (is_null($firstElement)) { $firstElement = 0; } if (is_null($lastElement)) { $lastElement = count($array) - 1; } if ($firstElement < $lastElement) { $middleElement = floor(($firstElement + $lastElement) / 2); $compareElement = $array[$middleElement]; $fromLeft = $firstElement; $fromRight = $lastElement; while ($fromLeft <= $fromRight) { while ($array[$fromLeft] < $compareElement) { $fromLeft++; } while ($array[$fromRight] > $compareElement) { $fromRight--; } if ($fromLeft <= $fromRight) { Jaws_ArraySort::QuickSortChangeElements($array, $fromLeft, $fromRight); $fromLeft++; $fromRight--; } } Jaws_ArraySort::QuickSort($array, $firstElement, $fromRight); Jaws_ArraySort::QuickSort($array, $fromLeft, $lastElement); } return true; }
/** * Performs case insensitive sort based on filename. * Directories first, followed by files. * * @access public * @param array $files The filesystem array * @param int $order * @return array the sorted filesystem array */ function SortFiles($files, $order = '') { if (empty($files)) { return $files; } $files = Jaws_ArraySort::SortBySecondIndex($files, 'is_dir', false, true); $filesStart = count($files); foreach ($files as $pos => $item) { if (!$item['is_dir']) { $filesStart = $pos; break; } } $dirs = array_splice($files, 0, $filesStart); if (empty($order)) { $order = $this->gadget->registry->fetch('order_type'); } $order = explode(',', $order); $indexs = array('title', 'filename', 'date'); if (!isset($order[0]) || !in_array($order[0], $indexs)) { $order[0] = 'filename'; $order[1] = false; } else { $order[1] = trim($order[1]) == 'true'; } if (!empty($files)) { $files = Jaws_ArraySort::SortBySecondIndex($files, $order[0], false, $order[1]); } if (!empty($dirs)) { $dirs = Jaws_ArraySort::SortBySecondIndex($dirs, 'filename', false); } return array_merge($dirs, $files); }