Set sorting of results. Use model field names as keys and Criteria's sort consntants.
Afields will be automatically decorated according to model.
For instance, when sorting on i18n field simply use field name, without language prefix.
Sort by title example:
$criteria = new Criteria();
$sort = [
'title' => Criteria::SortAsc
];
$criteria->setSort($sort);
If title is declared as i18n and language is set to en
, it will sort by title.en
ascending in this case.
Subsequent calls to setSort will override existing sort field and add new ones.
Sort by title and then reverse order and add another field example:
$criteria = new Criteria();
$sort = [
'title' => Criteria::SortAsc
];
$criteria->setSort($sort);
$sort = [
'title' => Criteria::SortDesc,
'active' => Critera::SortAsc
];
$criteria->setSort($sort);
Will sort by title descending, then active ascending
When using Sort
object as param, it will replace entire sorting
information with that provided by Sort
instance.
Sort by title and then replace with Sort
instance example:
$criteria = new Criteria();
$sort = [
'title' => Criteria::SortAsc
'active' => Critera::SortAsc
];
$criteria->setSort($sort);
$sort = new Sort([
'title' => Criteria::SortDesc,
];
$criteria->setSort($sort);
Will sort by title descending
Parameters
Returns