order(attributeOrAttributes, direction)

Orders the results of a search by the values of the specified CMS object attribute.

Applicable to the string, enum and date attribute types.

The attributes by which the CMS objects should be ordered must be contained in the objects themselves. Attributes in widgets part of the objects are not considered, meaning that you cannot have CMS objects ordered by an attribute belonging to a widget inside of the objects. This prevents unintended ordering results should the objects as well as their widgets contain attributes named identically.

There is a precision limit when sorting string values: Only the first 50 characters of a string are guaranteed to be considered when sorting search results.

Params

  • attributeOrAttributes (String or Array<String> | [String, 'asc' | 'desc']) – The attribute(s) by which the hits are to be sorted (e.g. '_path' or ['_objClass', 'title']).
    If more than one attribute is specified, the sort direction of each attribute can be set (e.g. [['_objClass', 'asc'], ['title', 'desc']]). If no direction is specified for an attribute, the default is asc. Up to five attributes are supported.
  • direction (optional) (String) – The direction in which the search results are to be ordered. Valid values are asc and desc (ascending or descending). The default is asc.

Returns

ObjSearch – Itself

Example

Find all CMS objects and order them by the date they were changed. Most recently changed objects are returned first:

Copy
query = Scrivito.Obj.all().order('_lastChanged', 'desc');

Find all CMS Objects and order them by _objClass and their title (from a to z):

Copy
query = Scrivito.Obj.all().order(['_objClass', 'title']);

Find all CMS Object and order them by _objClass and their title (from z to a):

Copy
query = Scrivito.Obj.all().order(['_objClass', ['title', 'desc']]);