Search.../

descending( )

Adds a sort to an aggregation, sorting by the items or groups by the specified properties in descending order.

Description

The descending() function refines a WixDataAggregate to sort the resulting items or groups in descending order. If you specify more than one property, descending() sorts the results in descending order by each property in the order they are listed.

You can sort the following types:

  • Number: Sorts numerically.
  • Date: Sorts by date and time.
  • String: Sorts lexicographically, so "abc" comes after "XYZ".
  • Reference: Compares by the ID of the referenced item as a String.

If a property contains a number as a String, that value will be sorted alphabetically and not numerically. Items that do not have a value for the specified sort property are ranked lowest.

Note: Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.

Authorization

Request

This endpoint does not take any parameters

Response Object

A WixDataAggregate object representing the refined aggregation.

Returns an empty object.

Status/Error Codes

Was this helpful?

Add a descending sort to an aggregation

Copy Code
1let newAggregate = aggregate.descending("state", "city");
Create an aggregation, add a descending sort, and run it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.aggregate("PopulationData")
6 .group("state", "year")
7 .avg("population")
8 .descending("populationAvg")
9 .run()
10 .then((results) => {
11 if (results.items.length > 0) {
12 let items = results.items; // see below
13 let numItems = results.length; // 6
14 let hasNext = results.hasNext(); // false
15 } else {
16 // handle case where no matching items found
17 }
18 })
19 .catch((error) => {
20 let errorMsg = error.message;
21 let code = error.code;
22 });
23
24/* Given the sample data above, items is:
25 * [
26 * {
27 * "_id": {"state": "NY","year": 2010},
28 * "populationAvg": 4226500,
29 * "state": "NY",
30 * "year": 2010
31 * }, {
32 * "_id": {"state": "NY","year": 2000},
33 * "populationAvg": 4153500,
34 * "state": "NY",
35 * "year": 2000
36 * }, {
37 * "_id": {"state": "CA","year": 2010},
38 * "populationAvg": 1969000,
39 * "state": "CA",
40 * "year": 2010
41 * }, {
42 * "_id": {"state": "CA", "year": 2000},
43 * "populationAvg": 2240000,
44 * "state": "CA",
45 * "year": 2000
46 * }, {
47 * "_id": {"state": "FL", "year": 2010},
48 * "populationAvg": 320500,
49 * "state": "FL",
50 * "year": 2010
51 * }, {
52 * "_id": {"state": "FL", "year": 2000},
53 * "populationAvg": 278500,
54 * "state": "FL",
55 * "year": 2000
56 * }
57 * ]
58 */