Introducing a new item dynamically to the list and organizing it by status followed by date. Here's the functional code:
I need the list to display items with false status in descending order of date first, then true status items sorted by date when adding a new item.
added item ={"Status":false,"Date":"2021-07-23T07:43:01.377Z","Title":"test6"}
List Order:-
var itemlist = [{
"Status":true,"Date":"2021-07-23T07:28:23.841Z","Title":"test5"},{
"Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test4"},{
"Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test3"},{
"Status":true,"Date":"2021-07-23T06:46:34.614Z","Title":"test2"},{
"Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test1"},{
"Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test0"}];
Expected Outcome:-
var itemlist = [{
"Status":false,"Date":"2021-07-23T07:43:01.377Z","Title":"test6"},{
"Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test4"},{
"Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test3"},{
"Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test1"},{
"Status":true,"Date":"2021-07-23T07:28:23.841Z","Title":"test5"},{
"Status":true,"Date":"2021-07-23T06:46:34.614Z","Title":"test2"},{
"Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test0"}];
Actual Result:-
var itemlist = [{
"Status":false,"Date":"2021-07-23T07:03:12.736Z","Title":"test4"},{
"Status":false,"Date":"2021-07-23T07:02:01.901Z","Title":"test3"},{
"Status":false,"Date":"2021-07-22T14:33:41.351Z","Title":"test1"},{
"Status":false,"Date":"2021-07-23T07:43:01.377Z","Title":"test6"},{
"Status":true,"Date":"2021-07-23T07:28:23.841Z","Title":"test5"},{
"Status":true,"Date":"2021-07-23T06:46:34.614Z","Title":"test2"},{
"Status":true,"Date":"2021-07-16T06:28:41.568Z","Title":"Test0"}];
Sorting Method Used:-
var res=itemlist.sort(
(a, b) => (a.Status === b.Status ) ?
0 : (a.Status ? 1 : -1 || +new Date(b.Date)- +new Date(a.Date)));