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

时间:2016-04-06 07:39:32

标签: javascript angularjs json sorting

我正在尝试按日期 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);

5 个答案:

答案 0 :(得分:6)

您可以使用日期字符串进行排序,而日期为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>");
&#13;
&#13;
&#13;

答案 1 :(得分:2)

比较日期时应使用date.getTime()

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分:2)

您的代码很好,只是因为您在比较中错过了第二个new Date()

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

应该是:

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

现在的方式,您只需将Date对象与string进行比较。

答案 3 :(得分:2)

实现这一目标有多种方法。其中,最简单的方法是将字符串转换为日期并将它们相互减去以获得负数,正数或零数:

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

通常认为sort函数需要返回-110。这是不正确的。它将根据数字是正面否定来对项目进行排序。 ECMAScript specification将其声明为:

  

如果comparefn未定义,则它应该是一个接受两个参数x和y的函数,如果x&lt;则返回负值。 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);

答案 4 :(得分:-1)

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