0

I have multiple json and want to display information by looping it. But I want to display information by latest date. sample data: here is sample data

var data = JSON.parse(CustomerProfile.custom.BackInStockData);
var bisData = bisData1.sort(function(a,b){return Math.abs(new Date(a.createdAt) - new Date(b.createdAt))});

It should display results like: Send Jul 08, 2019 Send Jul 08, 2019 Send Jul 05, 2019 Send Jul 05, 2019 Send Jul 04, 2019

Sampel Data:

User863
  • 19,346
  • 2
  • 17
  • 41
Rmage
  • 17
  • 1
  • 7
  • 3
    hint: `Math.abs` this will never sort, as your compare functions is always going to be positive.. just do -> `return a.createdAt.getTime() - b.createdAt.getTime()` – Keith Jul 08 '19 at 14:45
  • Please include the sample data as a code block instead of an image. It makes things easier for people to help you. – Edward Minnix Jul 08 '19 at 15:21
  • *bisData1* has not been assigned a value, so the code throws a reference error. If *bisData1* is replaced with *data*, then *bisData* and *data* will both reference the same array. Post the sample data as code, not a linked image. – RobG Jul 08 '19 at 20:38

2 Answers2

0

Replace this line:

bisData1.sort(function(a,b){return Math.abs(new Date(a.createdAt) - new Date(b.createdAt))});

with this:

bisData1.sort(function(a,b){ return new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime() });

and it should work

Devashish
  • 1,260
  • 1
  • 10
  • 21
  • 1
    A good answer should explain why the OP has their issue and how your code fixes it. Calling *getTime* will not make any difference in this case as the `-` operator coerces Dates to Number (their time value) anyway. – RobG Jul 08 '19 at 20:35
0

The OP has reference errors as bisData1 is not declared or assigned a value.

Assuming that is fixed and an array is assigned to bisData1, the assigning the result of bisData1.sort to bisData creates two references to the same array.

As noted in comments, you should not use Math.abs as sort requires a result that is a negative, zero or positive number.

An incorrect property name has been used when trying to reference the "Created At" property.

All code required to reproduce the issue should be posted in the question as text, not as images.

The linked code image has syntax errors. What you might have meant is something like:

let data = [
{"Created At": "Mon Jul 04 2019 13:05:21 GMT-0000 (GMT)","status":"send"},
{"Created At": "Mon Jul 08 2019 13:06:02 GMT-0000 (GMT)","status":"send"},
{"Created At": "Mon Jul 08 2019 14:07:59 GMT-0000 (GMT)","status":"send"},
{"Created At": "Mon Jul 05 2019 13:27:17 GMT-0000 (GMT)","status":"send"},
{"Created At": "Mon Jul 05 2019 13:27:17 GMT-0000 (GMT)","status":"send"}];

data.sort(function(a,b){return new Date(a['Created At']) - new Date(b['Created At'])});

console.log(data);

Which works as expected and sorts the data array.

In regard to parsing strings with the built–in parser, seeWhy does Date.parse give incorrect results? and MDN: Date.parse.

RobG
  • 142,382
  • 31
  • 172
  • 209