Back to Loopback Next

Order filter

docs/site/Order-filter.md

4.0.0-alpha.11.5 KB
Original Source

An order filter specifies how to sort the results: ascending (ASC) or descending (DESC) based on the specified property.

Node.js API

Order by one property:

<pre> {order: '<i>propertyName</i> <ASC|DESC>'} </pre>

Order by two or more properties:

<pre> {order: ['<i>propertyName</i> <ASC|DESC>', '<i>propertyName</i> <ASC|DESC>',...]} </pre>

Where:

  • propertyName is the name of the property (field) to sort by.
  • <ASC|DESC> signifies either ASC for ascending order or DESC for descending order.

REST API

Order by one property:

<pre> ?filter[order]=<i>propertyName</i>%20<ASC|DESC> </pre>

Order by two or more properties:

<pre> ?filter[order][0]=<i>propertyName</i> <ASC|DESC>&filter[order][1]=<i>propertyName</i> <ASC|DESC>... </pre>

Where:

  • propertyName is the name of the property (field) to sort by.
  • <ASC|DESC> signifies either ASC for ascending order or DESC for descending order.

You can also use stringified JSON format in a REST query.

{% include note.html content="Configure default ordering in default scope. " %}

Examples

Return the three most expensive items, sorted by the price property:

{% include code-caption.html content="Node.js API" %}

ts
await itemRepository.find({
  order: 'price DESC',
  limit: 3,
});

{% include code-caption.html content="REST" %}

/items?filter[order]=price%20DESC&filter[limit]=3

Or stringified JSON format:

/items?filter={"order":["price DESC"],"limit":3}