Double search & Sort?

Hello, i am trying to write a code for sorting searched results by price high to low…
For example, if customers searched for Bags, i want them to be able to sort these Bags by price :slight_smile: … my code so far is

export function sortbyShop_change(event) {
if ($w(‘#dropdown1’).value === ‘high’)
$w(“#dataset1”).setSort(
wixData.sort()
.descending(“price”));
if
($w(‘#dropdown1’).value=== ‘low’)
$w(“#dataset1”).setSort(
wixData.sort()
.ascending(“price”));
if
($w(‘#dropdown1’).value=== ‘none’)
$w(“#dataset1”).setSort(
wixData.sort()
);
}
function sort(sortOrder) {
let sort = wixData.sort();

if (sortOrder === “high”) {
sort = sort.descending(“price”);
}
else {
sort = sort.ascending(“price”);
}
$w(“#dataset1”).setSort(sort);
}
let sortOrder = $w(‘#dropdown1’).value;
sort(sortOrder);

The code doesn’t work so far :frowning: please someone adjust it for me. My coding skills are pretty limited :frowning:

up

Hi,
Your sort function should look like this(like the example code in the API ):

function sort(sortOrder) {
  let sort = wixData.sort();

  if(sortOrder === "high") {
    sort = sort.descending("price");
  }
  else {
    sort = sort.ascending("price");
  }

  $w("#myDataset").setSort(sort);
}

Instead Of all the code for your dropdown onChange event you should use the next code:

export function sortbyShop_change(event) {
let option = $w('#dropdown1').value;
sort(option);
}

Good luck :slight_smile: