Context
- I've just queried all offers a specific user is eligible for, and have stored those objects in an array.
- I'm now looking to sort them
by priority (
pri) so that I can place the first three on the user's dashboard - The sorting isn't happening, and I'm struggling to figure out why.
Code
var offers = new Object;
var offers = snap.data().offers;
var offerpriority = new Array;
offers.forEach(offer => {
db.collection("offers").doc(offer).get().then(snap => {
var thisoffer = {title: snap.data().title, pri: snap.data().priority};
offerpriority.push(thisoffer);
});
});
function compare(a, b){
return b.pri-a.pri;
};
console.log(offerpriority.sort(compare));