如何按日期对$ ​​.each()函数进行排序

时间:2018-12-20 06:25:01

标签: jquery arrays sorting

基本上,我需要对$ .each(messages,function(){})进行排序;按日期从我的数组。我尝试过的方法没有用,并且努力寻找一种好的方法。

我主要尝试使用sort函数对日期进行排序,但是在控制台中出现错误,提示它不是函数,或者根本不执行任何操作。

var messages = [
        {
            contact_name: "Josef Braga",
            time: "2018-12-20 01:53:49",
            isRead: 1,
        },
        {
            contact_name: "289-1445",
            time: "2018-12-20 02:53:49",
            isRead: 0,
        },
    ];

    $(".phone-content").append("<div class='phone-inner'></div>");
    $.each(messages, function () {
        $(".phone-inner").append("" +
            "<div data-date='" + this.time + "' class='phone-message'>" +
            "                    <div class='mtop'>" +
            "                        <p class='c-left " + (!this.isRead ? "new-message" : "") + "'>" + this.contact_name + "</p>" +
            "                        <p class='c-right'>" + relative_time(this.time) + "</p>" +
            "                    </div>" +
            "                    <div class='mdesc'>" +
            "                        <p>Dette er en ny besked! Hold da kæft hvor fedt mayn!</p>" +
            "                    </div>" +
            "                    <hr />" +
            "                </div>" +
            "            </div>");
    });

预期结果应该是最新消息排在最前面-但它只会按照在message数组中设置的方式对其进行排序。

2 个答案:

答案 0 :(得分:1)

幸运的是,您的time属性的格式很容易-只需调用sort并使用localeCompare来检查哪个.time首先出现(假设它们是始终采用YYYY-MM-DD hh:mm:ss格式:

var messages = [{
    contact_name: "Josef Braga",
    time: "2018-12-20 01:53:49",
    isRead: 1,
  },
  {
    contact_name: "289-1445",
    time: "2017-12-20 02:53:49",
    isRead: 0,
  },
  {
    contact_name: "289-1445",
    time: "2018-12-20 02:45:49",
    isRead: 0,
  },
  {
    contact_name: "289-1445",
    time: "2018-12-20 03:01:49",
    isRead: 0,
  },
  {
    contact_name: "289-1445",
    time: "2018-12-20 02:53:49",
    isRead: 0,
  },
];
messages.sort((a, b) => b.time.localeCompare(a.time));
console.log(messages);

答案 1 :(得分:1)

在您的情况下,时间是一个字符串,因此按字母顺序排序。您需要先将时间转换为时间戳(将是数字),然后再对其进行排序。

例如,假设您的邮件数组如下所示:

var messages = [
    {
        contact_name: "Josef Braga",
        time: "2018-12-20 01:53:49",
        isRead: 1,
    },
    {
        contact_name: "289-1445",
        time: "2018-12-20 02:53:49",
        isRead: 0,
    },
    {
        contact_name: "289-1445",
        time: "2018-12-19 02:53:49",
        isRead: 0,
    },
    {
        contact_name: "289-1445",
        time: "2018-12-19 02:54:49",
        isRead: 0,
    }
];

使用此功能进行排序:

messages.sort(function(a, b){return Date.parse(a.time) - Date.parse(b.time)});

将输出:

    [{
        contact_name: "289-1445",
        time: "2018-12-19 02:53:49",
        isRead: 0,
    },
    {
        contact_name: "289-1445",
        time: "2018-12-19 02:54:49",
        isRead: 0,
    }
    {
        contact_name: "Josef Braga",
        time: "2018-12-20 01:53:49",
        isRead: 1,
    },
    {
        contact_name: "289-1445",
        time: "2018-12-20 02:53:49",
        isRead: 0,
    }]

然后您可以执行$ .each。 希望这就是您想要的。