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 afterfalse
.
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
The fields used in the sort.
Was this helpful?
1let newSearch = search2 .documentType("Stores/Products")3 .descending("sku");
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.
1import wixSearch from 'wix-search';23// ...45wixSearch.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 });1920/** Example order of search result documents:21 *22 * Likes Views23 * 10 2324 * 7 2425 * 7 1826 * 3 2927 * 0 3128 * 0 1229 *30 */3132