Currently attempting to sort an array, however, the sorting function doesn't seem to be working as expected based on the console log output. I'm puzzled as to why it's not executing properly. Interestingly, the same code runs fine in simple JavaScript. Any insight into what might be going wrong would be greatly appreciated.
fetchContacts({
commit
}, userId) {
let contactsArr = [];
console.log('contact action triggered');
db.collection("users").doc(userId).collection("contacts").onSnapshot((snapshot) => {
snapshot.forEach((el) => {
contactsArr.push({
name: el.data().name,
phone: el.data().phone,
email: el.data().email,
id: el.id
});
})
})
console.log('contact array: '+ contactsArr);
console.log(contactsArr);
contactsArr.sort((a, b) => {
console.log('sorting in ');
const n1 = a.name.toLowerCase();
console.log(n1);
const n2 = b.name.toLowerCase();
console.log(n2);
if (n1 > n2) {
return 1;
} else if (n2 > n1) {
return -1;
} else {
return 0;
}
});
commit('setContacts', contactsArr);
},
https://i.sstatic.net/ybIGv.png