首先,如果这个问题不相关,我很抱歉。我不熟悉 ecma & javascript。我有 2 个对象。
我想合并它们并使用日期字段进行排序。完美地合并.push()
但是当我试图用add_date
价值对其进行排序时,我得到了意想不到的结果。
第一个对象:
[
{
"type": "Sipari\u015f - # PKXLF1",
"note": "",
"add_date": "2019-01-07 01:35:25",
"value": 448.4
},
{
"type": "Sipari\u015f - # PTO3U8",
"note": "",
"add_date": "2019-06-25 20:39:44",
"value": 472
},
{
"type": "Sipari\u015f - # PTYE1M",
"note": "",
"add_date": "2019-07-01 09:56:10",
"value": 1320.77
},
{
"type": "Sipari\u015f - # PXF079",
"note": "",
"add_date": "2019-09-06 16:57:09",
"value": 1859.68
},
{
"type": "Sipari\u015f - # PZ62E5",
"note": "",
"add_date": "2019-10-10 18:13:17",
"value": 755.2
}
]
第二个对象;
[
{
"type": "Tahsilat - Havale \/ Eft",
"note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
"add_date": "2019-01-22 01:35:25",
"value": 448.4
},
{
"type": "Tahsilat - Havale \/ Eft",
"note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
"add_date": "2019-08-02 20:24:57",
"value": 1000
},
{
"type": "Tahsilat - Havale \/ Eft",
"note": "Ziraat Bankas\u0131 Gelen Havale\/EFT",
"add_date": "2019-10-04 14:48:55",
"value": 792
}
]
到目前为止我已经尝试过什么;
这个问题的公认答案
array.sort(function(a,b){
// Turn your strings into dates, and then subtract them
// to get a value that is either negative, positive, or zero.
return new Date(b.date) - new Date(a.date);
});
我已经尝试过,但它给出了意想不到的结果;
0: {type: "Sipariş - # PKXLF1", note: "", add_date: "2019-01-07 01:35:25", value: 448.4}
1: {type: "Sipariş - # PTO3U8", note: "", add_date: "2019-06-25 20:39:44", value: 472}
2: {type: "Sipariş - # PTYE1M", note: "", add_date: "2019-07-01 09:56:10", value: 1320.77}
3: {type: "Sipariş - # PXF079", note: "", add_date: "2019-09-06 16:57:09", value: 1859.68}
4: {type: "Tahsilat - Havale / Eft", note: "Ziraat Bankası Gelen Havale/EFT", add_date: "2019-01-22 01:35:25", value: 448.4}
5: {type: "Tahsilat - Havale / Eft", note: "Ziraat Bankası Gelen Havale/EFT", add_date: "2019-08-02 20:24:57", value: 1000}
我错过了什么?我的add_date
领域有wrong date values
吗?
任何帮助都非常有用!