按日期javascript对JSON数据进行排序

Sort JSON data by date javascript

本文关键字:排序 数据 JSON 日期 javascript      更新时间:2024-01-14

我正在尝试按datejson数据进行排序,但不起作用。这是我正在尝试的。请纠正我犯错误的地方

样本代码

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