按日期对JSON数据排序javascript

按日期对JSON数据排序javascript,javascript,angularjs,json,sorting,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

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

您可以使用日期字符串进行排序,而它是一个日期

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”:“borderColor”:“000000”,“textColor”:“fff”},“id”:17608,“title”,“ghi”:“ghi”:”“2016-04-08 06:13:00.0”,“背景色”:“#000000”,“边框色”:“#000000”,“文本色”:“#fff”}”;
温度排序(功能(a、b){
返回a.start.localeCompare(b.start);
});

document.write(“+JSON.stringify(temp,0,4)+”);
您可以使用日期字符串进行排序,而它是一个日期

return new Date(a.start) > (b.start) ? 1 : -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”:“borderColor”:“#000000”,“textColor”:“#fff”},{“id”:17608,“title”:“ghi”,“start”:“ghi”:2016-04-08 06:13:00.0,“背景色”:“#000000”,“边框色”:“#000000”,“文本色”:“#fff”}”;
温度排序(功能(a、b){
返回a.start.localeCompare(b.start);
});

document.write(“+JSON.stringify(temp,0,4)+”);
在比较日期时应该使用
date.getTime()

return new Date(a.start) > (b.start) ? 1 : -1;
var-temp=[{id:17608,标题:“abc”,开始:“2016-03-23 06:13:00.0”,背景颜色:“000000”,边框颜色:“000000”,文本颜色:“fff”},{id:17608,标题:“def”,开始:“2016-04-13 06:13:00.0”,背景颜色:“000000”,边框颜色:“000000”,文本颜色:“fff”},{id:17608,标题:“ghi”,开始:“2016-04-13:00”,边框颜色:“000000”,背景颜色:“fff”#000000”,文本颜色:“#fff”}”;
控制台日志(temp);
温度排序(功能(a、b){
var d1=新日期(a.start).getTime();
var d2=新日期(b.start).getTime();
返回d1d2?1:0;
});

console.log(temp);
在比较日期时,应该使用
date.getTime()

var-temp=[{id:17608,标题:“abc”,开始:“2016-03-23 06:13:00.0”,背景颜色:“000000”,边框颜色:“000000”,文本颜色:“fff”},{id:17608,标题:“def”,开始:“2016-04-13 06:13:00.0”,背景颜色:“000000”,边框颜色:“000000”,文本颜色:“fff”},{id:17608,标题:“ghi”,开始:“2016-04-13:00”,边框颜色:“000000”,背景颜色:“fff”#000000”,文本颜色:“#fff”}”;
控制台日志(temp);
温度排序(功能(a、b){
var d1=新日期(a.start).getTime();
var d2=新日期(b.start).getTime();
返回d1d2?1:0;
});

console.log(temp);
您的代码很好,只是在比较中缺少了第二个
新日期()

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

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

按照现在的方式,您只需将
日期
对象与
字符串
进行比较。您的代码很好,只是在比较中缺少了第二个
新日期()

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

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

按照现在的方式,您只需将
日期
对象与
字符串
进行比较,有多种方法可以实现这一点。其中,最简单的方法是将字符串转换为日期,并相互减去,以获得负数、正数或零数:

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);
通常认为排序函数需要返回
-1
1
0
。这根本不是真的。它将根据数字是正、负还是零对项目进行排序。状态为:

如果comparefn不是未定义的,那么它应该是一个接受两个参数x和y的函数,如果xy,则返回正值

完整示例:

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

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

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);
通常认为排序函数需要返回
-1
1
0
。这根本不是真的。它将根据数字是正、负还是零对项目进行排序。状态为:

如果comparefn不是未定义的,那么它应该是一个接受两个参数x和y的函数,如果xy,则返回正值

完整示例:

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

这不是JSON!这是一个包含对象的Javascript数组。这不是JSON!这是一个包含对象的Javascript数组。只能按年份排序。只能按年份排序。