0

First of all i'm sorry if this is question is not relevant. I'm not familiar with ecma & javascript. I have 2 objects.

I want to merge them and sort with date field. Merging perfectly with .push() But when i'm trying to sort it with add_date value, i'm getting unexpected results.

First Object:

[
    {
        "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
    }
]

And Second Object;

[
    {
        "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
    }
]

What i've tried so far;

How to sort an array by a date property

This question's accepted answer

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

I've tried but it giving unexpected result like;

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}

What am i missing? Does my add_date field has wrong date values?

Any help greatly appricated!

HddnTHA
  • 1,041
  • 3
  • 19
  • 38

3 Answers3

0

Did you name you merged array array? And the sort method you paste there's a property called date. Your object has the property "add_date". Change the array sort method to something like this:

const a = [
    {
        "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
    }
];

const b = [
    {
        "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
    }
];
const c = [...a, ...b];


c.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(a.add_date) - new Date(b.add_date);
});

console.log(c);
Armali
  • 18,255
  • 14
  • 57
  • 171
henrik123
  • 1,605
  • 13
  • 19
  • Thank you for answer. That's what i've done, but it's not working. – HddnTHA Oct 11 '19 at 08:35
  • 2
    If you run the code above, the array will be sorted by start date. Isn't that the expected result? – henrik123 Oct 11 '19 at 08:39
  • Actually i got the answer. That's not about sorting. Thanks for effort. – HddnTHA Oct 11 '19 at 08:46
  • `new Date(a.add_date)` returns an invalid Date in at least one commonly used browser. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) "2019-10-04 14:48:55" is not a format supported by ECMA-262 so parsing is implementation dependent. The format of the OP dates will sort correctly without parsing to Date objects. – RobG Oct 11 '19 at 11:35
0

The date strings in the objects are in a format that can be sorted lexically, there's no need to convert them to Dates.

You can merge simply using the spread syntax ... and sort using < and >, e.g.

let arrA = [
    {"add_date": "2019-01-07 01:35:25"},
    {"add_date": "2019-06-25 20:39:44"},
    {"add_date": "2019-07-01 09:56:10"},
    {"add_date": "2019-09-06 16:57:09"},
    {"add_date": "2019-10-10 18:13:17"}
];

let arrB = [
    {"add_date": "2019-01-22 01:35:25"},
    {"add_date": "2019-08-02 20:24:57"},
    {"add_date": "2019-10-04 14:48:55"}
]

// Merge and sort with newest date first
let arrC = [...arrA, ...arrB].sort((a, b) => a.add_date < b.add_date?  1 :
                                             a.add_date > b.add_date? -1 : 0);

console.log(arrC);

Note that this does not copy the objects, the elements of arrC are references to the same objects that are in arrA and arrB.

If you want to convert a string like "2019-06-25 20:39:44" to a Date, do not use the built–in parser. Use a simple parse function or a library as it's not a format supported by ECMA-262 so parsing is implementation dependent and at least one common browser will return an invalid Date.

RobG
  • 142,382
  • 31
  • 172
  • 209
-1

You have to convert the date format into timeStamp.then do sorting

vali
  • 1
  • 1