0

From the following code I am getting the appropriate output, but I want the data to come in sorted form i.e the most recent data must come on top. The data must be sorted in descending order according to the difference of current date and 'win_date'.I am new to node.js..earlier in PHP I used to do this by using function array_multisort. Can someone suggest the alternative to do the same in nodejs.

var post=[];

var sql2 ="SELECT `post_id`, `user_id`, `post`, `votes_count`, `win_date` FROM `tb_winning_posts` WHERE  `group_id`=?";
 connection.query(sql2,[groupId],function(err, result) {

 });

for(var i=0;i<result.length;i++)
{
var timeDiff = getTimeDifference(result[i].win_date);   //returns diff of cur date & given   date in day,hour,min
post.push({"post_id":result[i].post_id,"post":result[i].post,"votes":result[i].votes_count,"date":timeDiff}); 

}
console.log(post); //getting the data in order as stored in database

1 Answer 1

1

Based on the answer https://stackoverflow.com/a/8837511/1903116

post.sort(function(a, b){
    var keyA = a.date, keyB = b.date;
    if(keyA < keyB) return 1;
    if(keyA > keyB) return -1;
    return 0;
});
console.log (post);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.