Search.../

descending( )

Adds a sort to a search, sorting by the specified fields in descending order.

Description

The descending() function refines a WixSearchBuilder to sort in descending order of the specified properties. If you specify more than one property, descending() sorts the results in descending order by each field 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 before "XYZ".
  • Boolean: true comes after false.

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

Syntax

function descending(field: ...string): WixSearchBuilder

descending Parameters

NAME
TYPE
DESCRIPTION
field
string

The fields used in the sort.

Returns

A WixSearchBuilder object representing the refined search.

Return Type:

Was this helpful?

Add a descending sort to a search builder

Copy Code
1let newSearch = search
2 .documentType("Stores/Products")
3 .descending("sku");
Create a search builder, add a descending sort, and run it

This example demonstrates how to sort forum posts in descending order from most to least popular. Posts are sorted from most likes to least likes. If multiple posts have the same number of likes, they're sorted from most views to least views.

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5wixSearch.search()
6 .documentType("Forum/Content")
7 .descending("likeCount", "viewCount")
8 .find()
9 .then( (results) => {
10 if(results.documents.length > 0) {
11 let documents = results.documents;
12 } else {
13 console.log("No matching results");
14 }
15 })
16 .catch( (error) => {
17 console.log(error);
18 });
19
20/** Example order of search result documents:
21 *
22 * Likes Views
23 * 10 23
24 * 7 24
25 * 7 18
26 * 3 29
27 * 0 31
28 * 0 12
29 *
30 */
31
32