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.