这是我当前的 json 响应。我想做的是根据时间对其进行排序。
router.get('/getmessage/:uuid',function(req, res){
chatID = req.params.uuid;
var chat = database.ref('chats/'+ chatID);
id = _.split(chatID, '_', 2);
chat.once('value', function(snapshot){
dataSet = [];
snapshot.forEach(function(childsnapshot){
dataSet.push({
child: childsnapshot
})
})
newDataSet = dataSet.sort(function(a,b){
return a.child.val().time - b.child.val().time;
})
res.json(newDataSet);
})
});
这是我的脚本。我尝试使用 .sort 但是当我尝试 console.log(a.child.val().time - b.child.val().time)
它返回 NaN 但我仍然得到了当前结果..
这是我想要得到的结果。
最佳答案
您可以将 array#sort
与 string#localeCompare
一起使用。
var data = [{child:{time:'2018-02-21 16:41:30'}}, {child:{time:'2018-02-21 16:30:21'}}];
data.sort((a,b) => a.child.time.localeCompare(b.child.time));
console.log(data);
关于javascript - 如何使用 firebase 和 Node js 基于日期时间对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48901708/