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
Returns
Fulfilled - When the sort has been set. Rejected - An error message.
Return Type:
Related Content:
Was this helpful?
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.
1import wixData from 'wix-data';23// ...45$w("#myDataset").setSort( wixData.sort()6 .ascending("lastName")7 .descending("age")8);9
This example builds a sort based on certain conditions. It then uses the built sort to sort a dataset.
1import wixData from 'wix-data';23// ...45function sort(name, dob) {6 let sort = wixData.sort();78 if(name === "desc") {9 sort = sort.descending("name");10 }11 else {12 sort = sort.ascending("name");13 }1415 if(dob === "desc") {16 sort = sort.descending("dob");17 }18 else {19 sort = sort.ascending("dob");20 }2122 $w("#myDataset").setSort(sort);23}24
1import wixData from 'wix-data';23// ...45$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} );
1import wixData from 'wix-data';23// ...45$w("#myDataset").setSort( wixData.sort() );