简体   繁体   English

如何在 javascript 中对 JSON 日期进行排序?

[英]How do I sort a JSON date in javascript?

The problem I encounter is that the.sort function doesn't work in my code.我遇到的问题是.sort function 在我的代码中不起作用。 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();

I think you want to sort the messages array by the created_at date.我认为您想按created_at日期对messages数组进行排序。 If that is correct, you can do it in one line:如果这是正确的,您可以在一行中完成:

messages.sort(function (a, b) { return b.created_at - a.created_at; })

This assumes that created_at holds a Date object.这假设created_at拥有一个Date object。 If it is a string instead, then you have to parse it to a Date .如果它是一个字符串,那么您必须将其解析为Date

This is a best guess based on your code, but I think you need to sort messages based on the date, then write out the HTML这是基于您的代码的最佳猜测,但我认为您需要根据日期对消息进行排序,然后写出 HTML

function showAllMessages(messages) {
    //Sort messages based on the date
    messages.sort(function (m1, m2) { return new Date(m1.created_at) - new Date(m2.created_at) });

    var html = '';
    for (i = 0; i < messages.length; i++) {
        var name = messages[i].user_id;
        var content = messages[i].content;
        var date = messages[i].created_at;
        html += messageDiv('block', name + ': </br> ' + content + ' </br> ' + date + ' </br> ');
    }

    var container = document.getElementById('container');
    container.innerHTML += html;
}
date.sort(function (a, b) { return new Date(b.start).getTime() = new Date(a.start).getTime() });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM