Help with on page sorting

Hello, I have a page where I am presenting offers to users. I am trying to create a button that the user can click after the page has loaded to sort the deals from lowest to highest price. I know I can sort using the dataset settings, but I want to do it with code so that I can add more buttons to sort by different values in the dataset. Full disclosure, this is my first time trying to write code!

Button I want to use - #button87
Dataset I want it to sort - #dataset5
Action I want it to take - sort clientFacingBid (it is set to a number field in dataset) from lowest to highest

When I click the button now, nothing happens. Any help is appreciated!

Below is what I have written:
import wixData from ‘wix-data’;

$w(“#button87”).onClick(() => {
$w(“#dataset5”).setSort(
wixData.sort()
.descending(“clientFacingBid”)
);
});

Is the code that you have provided written inside the onReady function for the page?
If not, then you’ll need to add this function via Properties Panel of the button. Right-click the button, choose View Properties, then click the “+” sight next to onClick, which will add a template for the future function.
After doing so you can copy+paste the content of your function, so it will look something like this:

import wixData from ‘wix-data’;

export function button87_click(event) {
$w(“#dataset5”).setSort
(wixData.sort()
.ascending(“clientFacingBid”)
)};

If this doesn’t help, then you’ll need to check a few other things. For example, if you are using a repeater to display your items, then please check that all of the relevant elements within the repeater are connected to data.

I also recommend you to check the Developer’s Console when previewing your site to see if there are any errors that might indicate the issue. You can find more information about it here: https://support.wix.com/en/article/corvid-testing-and-debugging-your-code

Thank you for getting back to me! I actually found the below code that I have added for all three buttons. I am running into a problem with ‘.ascending’ though. The pricing doesn’t seem to be sorting properly. If you want to see - you can see it here .

Do you know why this may be the case?

My field is set to number. The other buttons seem to be working properly. Any hel pis appreciated!

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(“#button87”).onClick( (event, $w) => {
$w(“#dataset5”).setSort( wixData.sort()
.ascending(“clientFacingBids”) );
} );

$w(“#button88”).onClick( (event, $w) => {
$w(“#dataset5”).setSort( wixData.sort()
.descending(“yearsExperience”) );
} );

$w(“#button89”).onClick( (event, $w) => {
$w(“#dataset5”).setSort( wixData.sort()
.descending(“turnaroundTime”) );
} );
} );