按时间戳排序JSON数据

时间:2013-10-29 16:09:54

标签: javascript jquery json

使用jquery或普通javascript按时间戳对它们进行排序的最佳方法是什么?

 [{"userName":"sdfs","conversation":"jlkdsjflsf","timestamp":"2013-10-29T15:30:14.840Z"},{"userName":"sdfs","conversation":"\ndslfkjdslkfds","timestamp":"2013-10-29T15:30:15.439Z"},{"userName":"sdfs","conversation":"\nfdsfjdsf","timestamp":"2013-10-29T15:30:15.883Z"},{"userName":"sdfs","conversation":"\ndsf","timestamp":"2013-10-29T15:30:16.010Z"},{"userName":"sdfs","conversation":"\ndsf","timestamp":"2013-10-29T15:30:16.186Z"},{"userName":"sdfs","conversation":"\nds","timestamp":"2013-10-29T15:30:16.339Z"},{"userName":"sdfs","conversation":"\nfds","timestamp":"2013-10-29T15:30:16.545Z"},{"userName":"sdfs","conversation":"\nf","timestamp":"2013-10-29T15:30:16.580Z"},{"userName":"sdfs","conversation":"\nds","timestamp":"2013-10-29T15:30:16.774Z"},{"userName":"sdfs","conversation":"\nfdsfjdsflkejlrjew","timestamp":"2013-10-29T15:30:17.739Z"},{"userName":"sdfs","conversation":"\nrewljrlew","timestamp":"2013-10-29T15:30:18.320Z"},{"userName":"sdfs","conversation":"\nfdslkfjdsf","timestamp":"2013-10-29T15:30:18.760Z"},{"userName":"sdfs","conversation":"\ndslfjewr","timestamp":"2013-10-29T15:30:19.287Z"},{"userName":"sdfs","conversation":"\newjrlkjef","timestamp":"2013-10-29T15:30:19.792Z"},{"userName":"sdfs","conversation":"\nsfj;dsajflkdsjfds","timestamp":"2013-10-29T15:30:20.548Z"},{"userName":"sdfs","conversation":"\nf","timestamp":"2013-10-29T15:30:20.720Z"},{"userName":"sdfs","conversation":"\ndsafdslkfjdslkjf","timestamp":"2013-10-29T15:30:21.855Z"},{"userName":"sdfs","conversation":"\ndsjflkdsfj","timestamp":"2013-10-29T15:30:21.175Z"},{"userName":"sdfs","conversation":"\ndsjfldsakf","timestamp":"2013-10-29T15:30:22.687Z"},{"userName":"sdfs","conversation":"\ndsafjlkdsjf","timestamp":"2013-10-29T15:30:22.335Z"},{"userName":"sdfs","conversation":"\nflsdjflkew","timestamp":"2013-10-29T15:30:36.848Z"},{"userName":"sdfs","conversation":"\nds","timestamp":"2013-10-29T15:30:23.723Z"},{"userName":"sdfs","conversation":"\nfjdsjgfdsgj","timestamp":"2013-10-29T15:30:38.048Z"},{"userName":"sdfs","conversation":"\nrewjf","timestamp":"2013-10-29T15:30:37.292Z"},{"userName":"sdfs","conversation":"\nfdjgfdjg","timestamp":"2013-10-29T15:30:38.688Z"},{"userName":"sdfs","conversation":"\nfdjg","timestamp":"2013-10-29T15:30:38.285Z"},{"userName":"sdfs","conversation":"\ntjreltjregjlkfjgfdjglkfdjg","timestamp":"2013-10-29T15:30:40.202Z"},{"userName":"sdfs","conversation":"\nre","timestamp":"2013-10-29T15:30:38.812Z"},{"userName":"sdfs","conversation":"\ngre","timestamp":"2013-10-29T15:30:41.478Z"},{"userName":"sdfs","conversation":"\nrejtrekjgjrgkjregkjre","timestamp":"2013-10-29T15:30:41.295Z"},{"userName":"sdfs","conversation":"\negj","timestamp":"2013-10-29T15:30:42.156Z"},{"userName":"sdfs","conversation":"\njglkrjgr","timestamp":"2013-10-29T15:30:41.925Z"},{"userName":"sdfs","conversation":"\nrglkjre","timestamp":"2013-10-29T15:30:42.993Z"},{"userName":"sdfs","conversation":"\ngregjrgj","timestamp":"2013-10-29T15:30:43.646Z"},{"userName":"sdfs","conversation":"\nrejg","timestamp":"2013-10-29T15:30:42.368Z"},{"userName":"sdfs","conversation":"\nrjg","timestamp":"2013-10-29T15:30:42.547Z"},{"userName":"sdfs","conversation":"lkjlkj","timestamp":"2013-10-29T16:04:09.149Z"},{"userName":"sdfs","conversation":"\nlkjlkjllk","timestamp":"2013-10-29T16:04:10.916Z"},{"userName":"sdfs","conversation":"\ndsjglfdj","timestamp":"2013-10-29T15:30:44.099Z"},{"userName":"sdfs","conversation":"\ndsf","timestamp":"2013-10-29T15:30:45.568Z"},{"userName":"sdfs","conversation":"\nwhat is leas goingt","timestamp":"2013-10-29T16:04:21.985Z"},{"userName":"sdfs","conversation":"\nthis figure out?","timestamp":"2013-10-29T16:04:26.850Z"},{"userName":"sdfs","conversation":"\nlkjlkjlk","timestamp":"2013-10-29T16:04:13.043Z"},{"userName":"sdfs","conversation":"\nnice to see you guys","timestamp":"2013-10-29T16:04:17.499Z"},{"userName":"sdfs","conversation":"\nwhat baout some baggles","timestamp":"2013-10-29T16:05:23.896Z"},{"userName":"sdfs","conversation":"\nwhat can you tell about baggles","timestamp":"2013-10-29T16:05:30.548Z"}]

例如,如果我加载到json数据之上,我会得到无序的时间戳,如下面的快照所示。我从ehcache(无序)获取此数据,我想通过时间戳在客户端订购它。

enter image description here

这是jsfiddle link,如果你在结果中看到时间戳没有按升序排序。

代码:

 $(document).ready(function () {
   var json = //json object above
   for (var i = 0; i < json.length; i++) {
        tr = $('<tr/>');
        tr.append("<td>" + (i+1) + "</td>");
        tr.append("<td>" + json[i].userName + "</td>");
        tr.append("<td>" + json[i].conversation + "</td>");
        tr.append("<td>" + json[i].timestamp + "</td>");
        $('table').append(tr);
     } 
    });

1 个答案:

答案 0 :(得分:3)

您的日期格式可以按字母顺序排序,因此您只需使用sort并直接比较字符串而无需转换为日期。

json.sort(function (a, b) {
    if (a.timestamp > b.timestamp){
      return 1;
    } else if (a.timestamp < b.timestamp){
      return -1;
    } else {
        return 0;
    }
});

Information on sort