Hello everyone, could you help me? I am trying to show my database records in ascending order, but this is an object inside another and I want to organize by cashOut follows the photo of my DB.
I'm using the mongoose and expressjs below as I'm looking for the data:
var Serial = function(){
this.sumTotalBets = function(game){
var totalBet = 0;
game.findOne({gameStatus:'in progress'}, function(err, games){
if(err){throw err}
for(var i = 0; i < games.players.length; i++){
totalBet += Number(games.players[i].bet);
}
totalBet = totalBet.toFixed(8);
console.log('Soma de tudo ' + totalBet);
for(i = 0; i < games.players.length; i++){
var verify = Number(games.players[i].profit);
if(totalBet > verify.toFixed(8)){
totalBet -= verify;
console.log('Continua ' + i + ' ' + totalBet.toFixed(8));
}else{
console.log('Parou em ' + i + ' ' + totalBet.toFixed(8) + ' PayOut ' + (games.players[i].cashOut - 0.01).toFixed(2));
}
}
});
}
}
module.exports = new Serial();
RESULTS IN THE CONSOLE
Soma de tudo 0.00263300
Continua 0 0.00262765
Continua 1 0.00252380
Parou em 2 0.00252380 PayOut 100.98
Continua 3 0.00252180
Parou em 4 0.00252180 PayOut 1.99'
In short, I want to list players by cashOut in increasing order