The problem I encounter is that the .sort function doesn't work in my code.
(我遇到的问题是.sort函数在我的代码中不起作用。)
So I am wondering why and I also would like to know the solution.(所以我想知道为什么,我也想知道解决方案。)
Here is my full code.
(这是我的完整代码。)
So I have a set of json data with username, text message, and a date when the user texted.(所以我有一组带有用户名,短信和用户发短信日期的json数据。)
I am able to display the data, but now I want to order them by date.(我可以显示数据,但是现在我想按日期排序。)
/**
* Call to load the test messages.
*/
function loadTestMessages() {
var request = new XMLHttpRequest();
request.open('GET', 'data/testmessages.json', true);
request.onload = function () {
if (request.status == 200) {
var messages = JSON.parse(request.response);
showAllMessages(messages);
} else {
console.error("could not load testmessages.json");
}
};
request.onerror = function () {
console.error("could not load testmessages.json");
};
request.send();
}
/**
* Make a "block" div
*/
function messageDiv(kindClass, html) {
return '<div class="' + kindClass + '">' + html + '</div>';
}
/**
* Display all messages
* @param {Array} messages
*/
function showAllMessages(messages) {
for (i = 0; i < messages.length; i++) {
var name = messages[i].user_id;
var content = messages[i].content;
var date = messages[i].created_at;
var container = document.getElementById('container');
// date.sort(function (a, b) { return a - b });
// console.info(date);
container.innerHTML += messageDiv('block', name + ': </br> ' + content + ' </br> ' + date + ' </br> ');
}
}
loadTestMessages();
ask by Icognito translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…