2

I have one date range json and my json is unsorted so my date range is not come correct order.I have try 2 solution but not getting correct result.

Here Below This is json:

0: { Status : 1
 DateFrom : "2018-09-01 00:00" 
 DateTo : "2018-09-08 00:00" }
1: { Status : 1
 DateFrom : "2018-09-02 00:00" 
 DateTo : "2018-09-08 00:00" }
2: { Status : 1
 DateFrom : "2018-09-08 00:00" 
 DateTo : "2018-09-15 00:00" }
3: { Status : 1
 DateFrom : "2018-10-01 00:00" 
 DateTo : "2018-10-06 00:00" }
4: { Status : 1
 DateFrom : "2018-10-06 00:00" 
 DateTo : "2018-10-13 00:00" }
5: { Status : 1
 DateFrom : "2018-07-01 00:00" 
 DateTo : "2018-07-07 00:00" }
6: { Status : 1
 DateFrom : "2018-07-07 00:00" 
 DateTo : "2018-07-14 00:00" }
7: { Status : 1
 DateFrom : "2018-08-04 00:00" 
 DateTo : "2018-08-11 00:00" }

I have try below code with but not getting correct result.

var res = JSON.parse($("#hdfAvailabe").val());

//This one try
res.sort(function (left, right) {
return moment.utc(left.timeStamp).diff(moment.utc(right.timeStamp))
});

//This second try
res.sort(function (a, b) {
return a - b
})

This is my excepted output:

0: { Status : 1
 DateFrom : "2018-07-01 00:00" 
 DateTo : "2018-07-07 00:00" }
 1: { Status : 1
 DateFrom : "2018-07-07 00:00" 
 DateTo : "2018-07-14 00:00" }
2: { Status : 1
 DateFrom : "2018-08-04 00:00" 
 DateTo : "2018-08-11 00:00" }
3: { Status : 1
 DateFrom : "2018-09-01 00:00" 
 DateTo : "2018-09-08 00:00" }
4: { Status : 1
 DateFrom : "2018-09-02 00:00" 
 DateTo : "2018-09-08 00:00" }
5: { Status : 1
 DateFrom : "2018-09-08 00:00" 
 DateTo : "2018-09-15 00:00" }
6: { Status : 1
 DateFrom : "2018-10-01 00:00" 
 DateTo : "2018-10-06 00:00" }
7: { Status : 1
 DateFrom : "2018-10-06 00:00" 
 DateTo : "2018-10-13 00:00" }  

any one know how can do that please help me for this issue.

1 Answer 1

1

sort() is the correct method you need to use, however you need to compare the values of the DateFrom properties after converting them to Date objects, like this:

res.sort(function(a, b) {
  var aDate = new Date(a.DateFrom), bDate = new Date(b.DateFrom);
  return aDate - bDate;
});

var res = [{
  Status: 1,
  DateFrom: "2018-09-01 00:00",
  DateTo: "2018-09-08 00:00"
}, {
  Status: 1,
  DateFrom: "2018-09-02 00:00",
  DateTo: "2018-09-08 00:00"
}, {
  Status: 1,
  DateFrom: "2018-09-08 00:00",
  DateTo: "2018-09-15 00:00"
}, {
  Status: 1,
  DateFrom: "2018-10-01 00:00",
  DateTo: "2018-10-06 00:00"
}, {
  Status: 1,
  DateFrom: "2018-10-06 00:00",
  DateTo: "2018-10-13 00:00"
}, {
  Status: 1,
  DateFrom: "2018-07-01 00:00",
  DateTo: "2018-07-07 00:00"
}, {
  Status: 1,
  DateFrom: "2018-07-07 00:00",
  DateTo: "2018-07-14 00:00"
}, {
  Status: 1,
  DateFrom: "2018-08-04 00:00",
  DateTo: "2018-08-11 00:00"
}];

res.sort(function(a, b) {
  var aDate = new Date(a.DateFrom), bDate = new Date(b.DateFrom);
  return aDate - bDate;
});
console.log(res);

Sign up to request clarification or add additional context in comments.

2 Comments

Or the oneliner : res.sort((a, b) => new Date(a.DateFrom) - new Date(b.DateFrom))
Very nice - but be aware that it won't work in any version of IE :( That's the only reason I haven't fully adopted arrow functions yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.