How to filter a dataset with the best results appearing first?

I am trying to filter a dataset of charities based on the parameters selected. Right now I have it so that if I want charities that help “Animals” and are “Local”, I can display ones that help animals OR are local. Along with these results are ones that help animals AND are local — but they’re not necessarily the first ones that come up. I still want to display charities that don’t match every parameter (that’s why I’m not using .hasAll), but I want the best results to show up as the first rows in the dataset.

function filter(categories) {
 let newCategoriesFilterCheck = compareArrays(categories, lastFilterCategories);
 if (!newCategoriesFilterCheck) {
 let newFilter = wixData.filter();
 
 // categories is an array with all of the selected parameters
 
 if(categories.length!=0){
        newFilter = newFilter.hasAll('allTags', categories);
        console.log(newFilter);
        }
 
        $w('#dataset1').setFilter(newFilter).then(function () {
 let count = $w("#dataset1").getTotalCount();
            });
            lastFilterCategories = categories;
    }
}

Thank you!

I still want to display charities that don’t match every parameter (that’s why I’m not using .hasAll), but I want the best results to show up as the first rows in the dataset.
How do you define —> “best results” ?

Perhaps you want to use a sort-function ?
https://www.wix.com/velo/reference/wix-dataset/dataset/setsort

Hi, thank you so much for your reply! I really appreciate it.

By best results, I mean the ones that best fit the users’ selections.

For instance, if they select “Animal” and “Local”, I would still want a result with “Animal” and “National” to be displayed, but I would want all the “Animal” and “Local” results to come first.

In terms of using the sort-function, I was wondering if there are more options than ascending and descending? Otherwise, I don’t know how to filter the dataset so it prioritizes the rows that best match the users’ selections.

Thanks again :slight_smile: