【问题标题】:Sort array by timestamp in jquery在jQuery中按时间戳对数组进行排序
【发布时间】:2015-11-21 11:05:30
【问题描述】:

在 AJAX 响应中,我得到了具有多个节点的 JSON 数组。如何按时间戳对每个节点进行排序。

我试过的代码:

$$.ajax({
            type:'POST',
            url: "http://www.xyz.co/get_all_news.php",
            dataType: "JSON",
            data:{'email': window.localStorage["email"], 'business_id': localStorage.getItem("business_id")},
            success: function (jsondata1){
                    data= JSON.parse(jsondata1);

                    jsondata1.sort(function(x, y){
                        return x.created_at - y.created_at;
                    })

                    console.log(jsondata1);   //Error - Uncaught TypeError: jsondata1.sort is not a function                                            
            }   
        }); 

jsondata1 的值也是

    var jsondata1 = [

{"id":"1","body":"Bsjd djjdjd jdjdkd djjdjd jdjd","votes":"4","update_type":"7","created_at":"2015-11-21 02:03:41","name":"Nehil"},

{"id":"2","body":"#TestingBestNominations","votes":"1","update_type":"7","created_at":"2015-11-21 02:03:44","name":"Nehil"},

{"id":"1","name":"#milestone1","date":"0000-00-00","location":"Mumbai","story_body":"Hdjjdjdbj djfjjd djkdjd","short_link":"A0Ijv","created_at":"2015-11-19 05:09:41","path":"\/SupportData\/ImpalzB2B\/uploads\/90294930451447934978817.jpg","update_type":"3"},

{"id":"1","name":"Product 1","description":"Dbbxbxjjd fjkd","short_link":"CmR0X","created_at":"2015-11-19 05:28:34","path":"\/SupportData\/ImpalzB2B\/uploads\/90294930451447936111369.jpg","update_type":"4"}

]

按(created_at)排序,我得到的错误是

“未捕获的类型错误:jsondata1.sort 不是函数”。

【问题讨论】:

  • 什么是jsondata1sort之前,什么是data在JSON解析之后在函数中
  • jsondata1 的值是我在上面给出的,解析后它会给我每个节点的值
  • 如果函数中已经是数组,为什么还要解析呢?
  • 试试new Date(x.created_at) - new Date(y.created_at)
  • 您可能想在数据上调用sort()data.sort()

标签: javascript android jquery arrays json


【解决方案1】:

您可以将日期字符串(在本例中为ISO format)视为字符串并使用String.prototype.localeCompare 对其进行排序。

var jsondata1 = [
        { "id": "1", "body": "Bsjd djjdjd jdjdkd djjdjd jdjd", "votes": "4", "update_type": "7", "created_at": "2015-11-21 02:03:41", "name": "Nehil" },
        { "id": "2", "body": "#TestingBestNominations", "votes": "1", "update_type": "7", "created_at": "2015-11-21 02:03:44", "name": "Nehil" },
        { "id": "1", "name": "#milestone1", "date": "0000-00-00", "location": "Mumbai", "story_body": "Hdjjdjdbj djfjjd djkdjd", "short_link": "A0Ijv", "created_at": "2015-11-19 05:09:41", "path": "\/SupportData\/ImpalzB2B\/uploads\/90294930451447934978817.jpg", "update_type": "3" },
        { "id": "1", "name": "Product 1", "description": "Dbbxbxjjd fjkd", "short_link": "CmR0X", "created_at": "2015-11-19 05:28:34", "path": "\/SupportData\/ImpalzB2B\/uploads\/90294930451447936111369.jpg", "update_type": "4" }
    ];

jsondata1.sort(function (a, b) { return a.created_at.localeCompare(b.created_at); });
document.write('<pre>' + JSON.stringify(jsondata1, 0, 4) + '</pre>');

对于不同的日期类型,我建议在这里查看:Sort Javascript Object Array By Date

【讨论】:

  • 如果我想要 DSC 顺序(最新更新优先),那么这是否完美? jsondata1.sort(function (a, b) { return b.created_at.localeCompare(a.created_at); }); ?
  • 是的,只是颠倒ab
猜你喜欢
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
相关资源
最近更新 更多