1

使用 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 获取这些数据(无序),我想在客户端按时间戳对其进行排序。

在此处输入图像描述

这是 jsfiddle链接,如果您在结果中看到时间戳不是按升序排列的。

代码:

 $(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);
     } 
    });
4

1 回答 1

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;
    }
});

信息sort

于 2013-10-29T16:52:30.440 回答