Apply sort criteria to a discussion search.

@param AbstractSearch $search
@param array $sort
494023
protected function applySort(AbstractSearch $search, array $sort = null)
    {
        $sort = $sort ?: $search->getDefaultSort();

        if (is_callable($sort)) {
            $sort($search->getQuery());
        } else {
            foreach ($sort as $field => $order) {
                if (is_array($order)) {
                    foreach ($order as $value) {
                        $search->getQuery()->orderByRaw(snake_case($field).' != ?', [$value]);
                    }
                } else {
                    $search->getQuery()->orderBy(snake_case($field), $order);
                }
            }
        }
    }
207787
public function addOrderBy($sort, $order = null)
    {
        if (false !== strpos($sort, '.')) {
            return parent::addOrderBy($sort, $order);
        }

        if (true === in_array($sort, self::$joinCriteria)) {
            $sort = $this->getSectionAlias().'.'.$sort;
        } elseif (0 !== strpos($this->getAlias().'.', $sort)) {
            $sort = $this->getAlias().'.'.$sort;
        }

        return parent::addOrderBy($sort, $order);
    }
1086327
protected function querySort($query, $sort = [])
    {
        $sort = $sort ?: $this->sort;

        foreach ($sort as $attribute => $order) {
            $query->orderBy($attribute, $order);
        }

        return $query;
    }
282808
private function appendSort($sort)
    {
        $column = $sort;
        $direction = 'asc';

        if ($sort[0] == '-') {
            $column = substr($sort, 1);
            $direction = 'desc';
        }

        $this->sort[] = [
            'column'    => $column,
            'direction' => $direction,
        ];
    }
1145034
public function setDefaultSearch( $search )
    {
        $this->defaultSearch = array();

        foreach ( $search as $column => $spec )
        {
            $this->defaultSearch[$column] = $spec;
        }

        return $this;
    }
1092893
protected function createSearch(Search $search, array $filters, $locale)
    {
        if (0 < count($filters['excluded'])) {
            foreach ($filters['excluded'] as $uuid) {
                $search->addQuery(new TermQuery('uuid', $uuid), BoolQuery::MUST_NOT);
            }
        }

        $query = new BoolQuery();

        $queriesCount = 0;
        $operator = $this->getFilter($filters, 'tagOperator', 'or');
        $this->addBoolQuery('tags', $filters, 'excerpt.tags.id', $operator, $query, $queriesCount);
        $operator = $this->getFilter($filters, 'websiteTagsOperator', 'or');
        $this->addBoolQuery('websiteTags', $filters, 'excerpt.tags.id', $operator, $query, $queriesCount);

        $operator = $this->getFilter($filters, 'categoryOperator', 'or');
        $this->addBoolQuery('categories', $filters, 'excerpt.categories.id', $operator, $query, $queriesCount);
        $operator = $this->getFilter($filters, 'websiteCategoriesOperator', 'or');
        $this->addBoolQuery('websiteCategories', $filters, 'excerpt.categories.id', $operator, $query, $queriesCount);

        if (null !== $locale) {
            $search->addQuery(new TermQuery('locale', $locale));
        }

        if (array_key_exists('types', $filters) && $filters['types']) {
            $typesQuery = new BoolQuery();
            foreach ($filters['types'] as $typeFilter) {
                $typesQuery->add(new TermQuery('type', $typeFilter), BoolQuery::SHOULD);
            }
            $search->addQuery($typesQuery);
        }

        if (array_key_exists('structureTypes', $filters) && $filters['structureTypes']) {
            $strTypesQuery = new BoolQuery();
            foreach ($filters['structureTypes'] as $filter) {
                $strTypesQuery->add(new TermQuery('structure_type', $filter), BoolQuery::SHOULD);
            }
            $search->addQuery($strTypesQuery);
        }

        if (0 === $queriesCount) {
            $search->addQuery(new MatchAllQuery(), BoolQuery::MUST);
        } else {
            $search->addQuery($query, BoolQuery::MUST);
        }

        return $search;
    }
1056546
public function search(SearchCriteria $criteria, $limit = null, $offset = 0)
    {
        $actor = $criteria->actor;

        $query = $this->discussions->query()->select('discussions.*')->whereVisibleTo($actor);

        // Construct an object which represents this search for discussions.
        // Apply gambits to it, sort, and paging criteria. Also give extensions
        // an opportunity to modify it.
        $search = new DiscussionSearch($query->getQuery(), $actor);

        $this->gambits->apply($search, $criteria->query);
        $this->applySort($search, $criteria->sort);
        $this->applyOffset($search, $offset);
        $this->applyLimit($search, $limit + 1);

        $this->events->dispatch(new Searching($search, $criteria));

        // Execute the search query and retrieve the results. We get one more
        // results than the user asked for, so that we can say if there are more
        // results. If there are, we will get rid of that extra result.
        $discussions = $query->get();

        $areMoreResults = $limit > 0 && $discussions->count() > $limit;

        if ($areMoreResults) {
            $discussions->pop();
        }

        return new SearchResults($discussions, $areMoreResults);
    }
253958
public function search(array $search)
    {
        $rows = new Collection();

        if (empty($search)) {
            return $rows;
        }

        foreach ($this->getCart() as $item) {
            if (array_intersect_assoc($item->intersect($search)->toArray(), $search)) {
                $rows->put($item->__raw_id, $item);
            }
        }

        return $rows;
    }
157168
public function search(array $search, $sort, $select = null)
    {
        $select = ($select) ?: $this->getSelect();

        foreach ($search as $key => $value) {
            if (!$value['searchString'] == '') {
                if (substr($value['searchString'], 0, 1) == '=' && $key == 0) {
                    $id = (int)substr($value['searchString'], 1);
                    $select->where->equalTo($this->getPrimaryKey(), $id);
                } else {
                    $where = $select->where->nest();
                    $c = 0;

                    foreach ($value['columns'] as $column) {
                        if ($c > 0) $where->or;
                        $where->like($column, '%' . $value['searchString'] . '%');
                        $c++;
                    }

                    $where->unnest();
                }
            }
        }

        $select = $this->setSortOrder($select, $sort);

        return $this->fetchResult($select);
    }
576827
public function sort(array &$methods, $sort = self::BY_COMPLEXITY)
    {
        if (!in_array($sort, self::SORTS, true)) {
            $sort = self::BY_COMPLEXITY;
        }

        usort(
            $methods,
            $sort === self::BY_COMPLEXITY
                ? $this->byComplexity()
                : $this->byName()
        );
    }
316916
public static function sortArgs(array &$args) {
        ksort($args);
        foreach ($args as $key => $val) {
            if (is_array($val)) {
                ksort($val);
                $args [ $key ] = $val;
                self::sortArgs($val);
            }
        }
    }
1114219
public function orderBy($sort, $order = 'ASC') {
        if ($order == 'ASC' || $order == 'DESC') {
            $this->q->apd(' ORDER BY ' . $sort . ' ' .$order);
        } else {
            $this->setError('q', 'No valid order');
            return false;
        }

        return $this;
    }
52359
public function sort($sort)
    {
        if (null !== $sort && !is_array($sort)) {
            throw new \InvalidArgumentException(sprintf('The sort "%s" is not valid.', $sort));
        }

        $this->sort = $sort;

        return $this;
    }
160903
public function find( $search ) {

		if ( ! is_array( $search ) ) {
			$search = (array) $search;
		}

		foreach ( $search as $val ) {
			if ( $this->fulltextSearchInPost( $val ) !== false ) {
				return true;
			}
			if ( $this->fulltextSearchInMeta( $val ) !== false ) {
				return true;
			}
		}

		return false;
	}
1173749
private function processSortClauses(array $sortClausesArray, ParsingDispatcher $parsingDispatcher)
    {
        $sortClauses = [];
        foreach ($sortClausesArray as $sortClauseName => $sortClauseData) {
            if (!is_array($sortClauseData) || !isset($sortClauseData[0])) {
                $sortClauseData = [$sortClauseData];
            }

            foreach ($sortClauseData as $data) {
                $sortClauses[] = $this->dispatchSortClause($sortClauseName, $data, $parsingDispatcher);
            }
        }

        return $sortClauses;
    }
304731
private static function handleSort($query, $sort = null)
    {
        $replace = '';

        if ($sort !== null) {
            $sort ->validate(static::class);

            $tableAttribute = (
                $sort->table === null
                ? $sort->attribute
                : $sort->table . '"."' .$sort->attribute
            );

            $replace = "\n" . sprintf(
                'ORDER BY "%s" %s',
                $tableAttribute,
                ($sort->ascending ? 'ASC' : 'DESC')
            );
        }

        $query = str_replace(
            '{{sort}}',
            $replace,
            $query
        );

        return $query;
    }
216900
protected function processSorting(string $sorts, Criteria $criteria): void
    {
        $sortList = array_map('trim', explode(',', $sorts));
        $sortMap  = [];

        foreach ($sortList as $item)
        {
            if (strpos($item, '-') === 0) {
                $sortMap[substr($item, 1)] = Criteria::DESC;
                continue;
            }

            $sortMap[$item] = Criteria::ASC;
        }

        $criteria->orderBy($sortMap);
    }