I have two arrays, array1 has all objects and array2 has filtered objects based on a search string.
Currently I am rendering array2 (which will contain all objects from array1 when the search string is empty, but will return only filtered objects if search string isn't empty) to the user but I would like to show all objects and style the filtered ones (those which match the search) differently and keep those matching the search query in the top of the array/list and if I even in addition to that could also sort those matched objects alphabetically I would love to.
Here is how I am filtering based on the search query:
export default {
name: "RegionSelector",
data: () => ({
searchRegionTextValue: "",
regions: [
{
country: "USA",
flag: "flag-en-us",
name: "United States",
language: "English",
},
{
country: "UK",
flag: "flag-en-gb",
name: "United Kingdom",
language: "English",
},
{
country: "DE",
flag: "flag-de",
name: "Germany",
language: " German",
},
],
}),
methods: {
// Used in my v-for in the template to style non-matched results differently
checkRegion(region) {
var isInArray =
this.filteredRegions.find(function(el) {
return el === region;
}) !== undefined;
return isInArray;
},
computed: {
filteredRegions() {
function compare(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
}
let regions = this.regions.filter((region) => {
return (
region.name
.toLowerCase()
.indexOf(this.searchRegionTextValue.toLowerCase()) != -1 ||
region.language
.toLowerCase()
.indexOf(this.searchRegionTextValue.toLowerCase()) != -1
);
});
regions.sort(compare);
return regions;
},
},
};
And in my template I am rendering them this way (to show all objects but style them differently):
<div v-for="region in regions">
<span
:class="checkRegion(region) ? 'matched-query' : 'unmatched-query'">
{{region.name}}
</span>
</div>
How can I implement the sorting as mentioned above?