Page Contents

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

REST API

Order by one property: 

filter[order]=propertyName <ASC|DESC>

Order by two or more properties:

filter[order][0]=propertyName <ASC|DESC>&filter[order][1][propertyName]=<ASC|DESC>...

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

Node API

Order by one property:

{ order: 'propertyName <ASC|DESC>' }

Order by two or more properties:

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

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.

REVIEW COMMENT from Rand

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, …

Examples

Return the three loudest three weapons, sorted by the audibleRange property:

REST

/weapons?filter[order]=audibleRange%20DESC&filter[limit]=3

Node API

weapons.find({
  order: 'price DESC',
  limit: 3
});