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 に答える