You are here

public function Query::sort in Search API 8

Adds a sort directive to this search query.

If no sort is manually set, the results will be sorted descending by relevance.

Parameters

string $field: The ID of the field to sort by. In addition to all indexed fields on the index, the following special field IDs may be used:

  • search_api_relevance: Sort by relevance.
  • search_api_datasource: Sort by datasource.
  • search_api_language: Sort by language.
  • search_api_id: Sort by item ID.

string $order: The order to sort items in – one of the SORT_* constants.

Return value

$this

Overrides QueryInterface::sort

See also

\Drupal\search_api\Query\QueryInterface::SORT_ASC

\Drupal\search_api\Query\QueryInterface::SORT_DESC

File

src/Query/Query.php, line 462

Class

Query
Provides a standard implementation for a Search API query.

Namespace

Drupal\search_api\Query

Code

public function sort($field, $order = self::SORT_ASC) {
  $order = strtoupper(trim($order));
  $order = $order == self::SORT_DESC ? self::SORT_DESC : self::SORT_ASC;
  if (!isset($this->sorts[$field])) {
    $this->sorts[$field] = $order;
  }
  return $this;
}