Newscoop\Service\Implementation\ThemeServiceLocalFileSystem::sort PHP Method

sort() protected method

Sort the array.
protected sort ( array $array, string $property, $ascending ) : array
$array array The array of elements to be sorted.
$property string The method name that provides the sorting key.
return array The sorted array.
    protected function sort($array, $property, $ascending)
    {
        $isAllEqual = true;
        // going to use this to still simulate a sort if all values are equal
        usort($array, function ($a, $b) use($property, $ascending, &$isAllEqual) {
            $cmp = strcmp($a->{$property}(), $b->{$property}());
            if ($cmp != 0) {
                $isAllEqual = false;
            }
            return $ascending ? $cmp : -$cmp;
        });
        // if all values equal and sort is descending just reverse the results..
        if ($isAllEqual && !$ascending) {
            return array_reverse($array);
        }
        return $array;
    }