title | lang | layout | keywords | tags | sidebar | permalink | summary |
---|---|---|---|---|---|---|---|
Order filter |
en |
page |
LoopBack |
lb3_sidebar |
/doc/en/lb3/Order-filter.html |
An order filter specifies how to sort the results: ascending (ASC) or descending (DESC) based on the specified property.
Order by one property:
filter[order]=propertyName
Order by two or more properties:
filter[order][0]=propertyName &filter[order][1]=propertyName=...
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. " %}
{% include content/angular-methods-caveat.html lang=page.lang %}
Order by one property:
{ order: 'propertyName ' }
Order by two or more properties:
{ order: ['propertyName ', 'propertyName ',...] }
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.
I could not get multiple sort fields to work with REST. I would expect to be able to sort by (A,B) where it sorts by A and then by B:
filter[order]=propertyName <ASC|DESC>,propertyName <ASC|DESC>, ...
But for example
http://localhost:3000/api/cars?filter[order]=color%20ASC,id%20ASC
Does not seem to be sorted in any order.
Nor does http://localhost:3000/api/cars?filter[order][0]=color%20ASC[order][1]=make%20ASC
rfeng: filter[order][0]=color%20ASC&filter[order][1]=make%20ASC should be used.
Also: Is there any way to sort numerical properties as numbers instead of string? For example, if I sort by ID the records are returned in this order: 1, 10, 100, 101, 102, ..., 11, 110, 111, ...
Return the three loudest three weapons, sorted by the audibleRange
property:
REST
/weapons?filter[order]=audibleRange%20DESC&filter[limit]=3
{% include code-caption.html content="Node API" %}
weapons.find({
order: 'audibleRange DESC',
limit: 3
});