Collection.orderBy
/ Query.orderBy
Specify query sort criteria
The method signature is as follows:
function orderBy(fieldName: string, order: string): Collection | Query
The method accepts a required string parameter fieldName
to define the field to be sorted, and a string parameter to order
define the sort order. order
Can only take asc
or desc
.
If you need to sort the nested fields, you need to use "dot notation" to connect the nested fields, such as style.color
representing style
the nested fields in the field color
.
At the same time, it also supports sorting by multiple fields, which orderBy
can be called multiple times . The order orderBy
of multiple field sorting will sort multiple fields according to the calling order.
Sample code:
- Sort by a field : get to-do items in ascending order by progress
const db = wx.cloud.database()
db.collection('todos').orderBy('progress', 'asc')
.get()
.then(console.log)
.catch(console.error)
- Sort by multiple fields :
first sort byprogress
descending order (theprogress
bigger the higher the front), and then pressdescription
the ascending order (the alphabetical order the higher the front) to take the to-do list:
const db = wx.cloud.database()
db.collection('todos')
.orderBy('progress', 'desc')
.orderBy('description', 'asc')
.get()
.then(console.log)
.catch(console.error)