I am struggling to retrieve data from firebase and sort them according to the field 'overallScore' in descending order. Despite following suggestions like using array.sort(function(a, b) {return b.value - a.value), I have not been successful in sorting the array as expected.
The structure of my firebase data is illustrated here:
https://i.stack.imgur.com/ZNQ5M.png
Below is an excerpt of my code:
getAll: function(){
database.collection('Users').get().then(querySnapShot => {
let data = [];
querySnapShot.forEach(doc => {
data.push(doc.data())
})
this.datasets = data;
this.getTop();
})
},
getTop: function(){
var today = String(moment(String(new Date())).format("DDMMYYYY"));
var top = [];
for (var i = 0; i < this.datasets.length; i++) {
if (this.datasets[i].scoreDate == today){
top.push(this.datasets[i]);
}
}
top.sort(function(a, b){return b.overallScore - a.overallScore});
this.top10 = top;
},
Upon examining the console log result after running the code, the output appears as shown below: