2016-04-06 139 views
1

我想排序我的json數據日期但它不工作。這是我正在嘗試的。請糾正我在哪裏,我會犯錯按日期排序JSON數據javascript

示例代碼

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

這不是JSON!它是一個帶有對象的Javascript數組。 – deceze

回答

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

2

您應該使用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);

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

2

實現此目的有多種方式。其中,最簡單的是將字符串轉換爲日期和彼此。減去他們得到一個負數,正數或零號:

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

人們常常認爲,排序函數需要返回-110。這是不正確的。如果數量爲正數,負數零數。所述ECMAScript specification指出它爲:

如果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); 
-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); 
+0

只能按年份排序。 –