Search.../

setSort( )

Sets the dataset sort order.

Description

The setSort function sorts the items in the dataset.

The WixDataSort object is created by calling the sort() function on wixData and chaining one or more of the following Wix Data sort functions.

Setting a dataset's sort overrides existing sorts that were previously set using the setSort() function or using the Dataset Settings panel in the Editor.

To clear a dataset's current sort, call setSort() and pass it an empty sort. You create an empty sort by calling the sort() function without chaining any of the additional sort functions mentioned above.

Calling setSort() on a write-only dataset causes an error.

You will need to import wix-data to create a WixDataSort object.

Note:
When using a read-write dataset and linked input elements, calling any of the following functions will save any changes made in the linked input elements.

Syntax

function setSort(sort: WixDataSort): Promise<void>

setSort Parameters

NAME
TYPE
DESCRIPTION
sort

A wix-data sort object.

Returns

Fulfilled - When the sort has been set. Rejected - An error message.

Return Type:

Promise<void>

Related Content:

Was this helpful?

Set a dataset's sort

This example sorts a dataset by lastname field in ascending order and when more than one item has the same lastname value they are sorted by the age field in descending order.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5$w("#myDataset").setSort( wixData.sort()
6 .ascending("lastName")
7 .descending("age")
8);
9
Build and set a dataset's sort

This example builds a sort based on certain conditions. It then uses the built sort to sort a dataset.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5function sort(name, dob) {
6 let sort = wixData.sort();
7
8 if(name === "desc") {
9 sort = sort.descending("name");
10 }
11 else {
12 sort = sort.ascending("name");
13 }
14
15 if(dob === "desc") {
16 sort = sort.descending("dob");
17 }
18 else {
19 sort = sort.ascending("dob");
20 }
21
22 $w("#myDataset").setSort(sort);
23}
24
Set a dataset's sort

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5$w("#myDataset").setSort( wixData.sort()
6 .ascending("lastName")
7 .descending("age")
8)
9.then( () => {
10 console.log("Dataset is now sorted");
11} )
12.catch( (err) => {
13 console.log(err);
14} );
Clear a dataset's sorts

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5$w("#myDataset").setSort( wixData.sort() );