Search.../

ascending( )

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

Description

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

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 ascending(field: ...string): WixSearchBuilder

ascending 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 an ascending sort to a search

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

This example demonstrates how to sort forum content from earliest to latest.

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5wixSearch.search()
6 .documentType("Forum/Content")
7 .ascending("lastActivityDate")
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
Create a search, add filters, and run it

This example demonstrates how to search for store products that are on sale, are part of the "Spring" or "Summer" store collections, and how to sort the results in alphabetical order according to the SKU.

Copy Code
1import wixSearch from 'wix-search';
2
3// ...
4
5wixSearch.search()
6 .documentType("Stores/Products")
7 .eq("onSale", true)
8 .hasSome("collections", ["Spring", "Summer"])
9 .ascending("sku")
10 .find()
11 .then( (results) => {
12 if(results.documents.length > 0) {
13 let documents = results.documents;
14 } else {
15 console.log("No matching results");
16 }
17 })
18 .catch( (error) => {
19 console.log(error);
20 });
21
22