簡體   English   中英

按日期排序 JSON 數據 javascript

[英]Sort JSON data by date javascript

我正在嘗試按日期對我的json 數據進行排序,但它不起作用。 這就是我正在嘗試的。 請糾正我錯誤的地方

示例代碼

var temp = [{
    "id": 17608,
    "title": "abc",
    "start": "2016-03-23 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "def",
    "start": "2016-04-13 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "ghi",
    "start": "2016-04-08 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}];

console.log(temp);

temp.sort(function(a, b) {
    if (new Date(a.start) == new Date(b.start)) {
        return a.row == b.row ? 0 : +a.row > +b.row ? 1 : -1;
    }

    return new Date(a.start) > (b.start) ? 1 : -1;
});

console.log(temp);

您可以使用日期字符串進行排序,而日期字符串是ISO 6801日期。

 var temp = [{ "id": 17608, "title": "abc", "start": "2016-03-23 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }, { "id": 17608, "title": "def", "start": "2016-04-13 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }, { "id": 17608, "title": "ghi", "start": "2016-04-08 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }]; temp.sort(function (a, b) { return a.start.localeCompare(b.start); }); document.write("<pre>" + JSON.stringify(temp, 0, 4) + "</pre>"); 

比較日期時應使用date.getTime()

 var temp= [{id:17608,title:"abc",start:"2016-03-23 06:13:00.0",backgroundColor:"#000000",borderColor:"#000000",textColor:"#fff"},{id:17608,title:"def",start:"2016-04-13 06:13:00.0",backgroundColor:"#000000",borderColor:"#000000",textColor:"#fff"},{id:17608,title:"ghi",start:"2016-04-08 06:13:00.0",backgroundColor:"#000000",borderColor:"#000000",textColor:"#fff"}]; console.log(temp); temp.sort(function(a, b) { var d1 = new Date(a.start).getTime(); var d2 = new Date(b.start).getTime(); return d1<d2?-1:d1>d2?1:0; }); console.log(temp); 

您的代碼很好,只是您在比較中缺少第二個new Date()

return new Date(a.start) > (b.start) ? 1 : -1;

應該:

return new Date(a.start) > new Date(b.start) ? 1 : -1;

現在,您只需將Date對象與string比較即可。

有多種方法可以實現這一目標。 其中,最簡單的方法是將字符串轉換為日期,並將它們彼此相減以獲得負數,正數或零數:

temp.sort(function(a,b){
  return new Date(a.start) - new Date(b.start);
});

人們常常認為,排序函數需要返回-110 這是不正確的。 它將基於數字是正數負數還是對項目進行排序。 ECMAScript規范將其聲明為:

如果comparefn沒有定義,則它應該是一個接受兩個參數x和y的函數,如果x <y,則返回負值;如果x = y,則返回零;如果x> y,則返回正值。

完整示例:

var temp = [{
    "id": 17608,
    "title": "abc",
    "start": "2016-03-23 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "def",
    "start": "2016-04-13 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "ghi",
    "start": "2016-04-08 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}];

console.log(temp);

temp.sort(function(a,b){
  // Convert strings to dates and substract.
  // This way you get a value which is negative, positive or zero
  return new Date(a.start) - new Date(b.start);
});

console.log(temp);
  const groups = this.posts.reduce((groups, data) => {
    const date = data.published_date.split(' ')[0];
    if (!groups[date]) {
      groups[date] = [];
    }
    groups[date].push(data);
    return groups;
  }, {});

  // Edit: to add it in the array format instead
  const groupArrays = Object.keys(groups).map((date) => {
    return {
      date,
      posts: groups[date]
    };
  });

這樣做,它應該工作!

        var temp = [{
        "id": 17608,
        "title": "abc",
        "start": "2016-03-23 06:13:00.0",
        "backgroundColor": "#000000",
        "borderColor": "#000000",
        "textColor": "#fff"
    }, {
        "id": 17608,
        "title": "def",
        "start": "2016-04-13 06:13:00.0",
        "backgroundColor": "#000000",
        "borderColor": "#000000",
        "textColor": "#fff"
    }, {
        "id": 17608,
        "title": "ghi",
        "start": "2016-04-08 06:13:00.0",
        "backgroundColor": "#000000",
        "borderColor": "#000000",
        "textColor": "#fff"
    }];
    
    
    
    const groups = this.temp.reduce((groups, data) => {
        const date = data.start.split(' ')[0];
        if (!groups[date]) {
          groups[date] = [];
        }
        groups[date].push(data);
        return groups;
      }, {});
    
      // Edit: to add it in the array format instead
      const groupArrays = Object.keys(groups).map((date) => {
        return {
          date,
          temps: groups[date]
        };
      });

console.log(groupArrays)
var temp = [{
    "id": 17608,
    "title": "abc",
    "start": "2016-03-23 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "def",
    "start": "2016-04-13 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "ghi",
    "start": "2016-04-08 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}];

console.log(temp);

temp.sort(function(a, b) {
    return parseFloat(a.start) - parseFloat(b.start);
});

console.log(temp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM