How to add sorting to a collection page connected to a CMS table (by name or by rating)

https://www.prodivani.com/divany

This collection is connected to the CMS.
How can I add sorting by model name (A-Z; Z-A or by rating 1-100; 100-1) to this page?
Thanks

Use dropdown onChange to trigger dataset.setSort(). Two approaches depending on your setup:

If you have a dataset connected to your repeater, add a dropdown with sort options and this code:

import wixData from ‘wix-data’;

$w(‘#sortDropdown’).onChange((event) => {
const sortValue = event.target.value;

switch(sortValue) {
case ‘nameAsc’:
$w(‘#myDataset’).setSort(wixData.sort().ascending(‘modelName’));
break;
case ‘nameDesc’:
$w(‘#myDataset’).setSort(wixData.sort().descending(‘modelName’));
break;
case ‘ratingHigh’:
$w(‘#myDataset’).setSort(wixData.sort().descending(‘rating’));
break;
case ‘ratingLow’:
$w(‘#myDataset’).setSort(wixData.sort().ascending(‘rating’));
break;
}
});

Replace ‘modelName’ and ‘rating’ with your actual field IDs from the CMS collection.

If you’re loading data with wixData.query() instead of a dataset, chain .ascending() or .descending() directly on the query before .find().