I want to sort the date and time while pushing the items to an array. So far, I can sort just the date but not able to sort the time. If the same date has two times, then how do I sort the date first and then the timing for that date.
let items = [
{'id': 'id7', 'date': '2020-08-17', 'time': '05:02:54.000'},
{'id': 'id2', 'date': '2020-05-12', 'time': '02:09:57.000'},
{'id': 'id3', 'date': '2020-05-12', 'time': '03:04:44.000'}, //both the 2020-05-12 date has two timings so I want to sort these time too along with the date
{'id': 'id4', 'date': '2020-09-23', 'time': '07:16:23.000'}
];
const headers = [
'ID',
'Date',
'Time'
];
dataArr = []
items.forEach(item => {
dataArr.push([
item.id,
item.date,
item.time
]);
})
;
let sortedData = [headers, ...dataArr.sort((a, b) => a[1].localeCompare(b[1]))];
final sorted array must look something like
['id': 'id2', 'date': '2020-05-12', 'time': '02:09:57.000'],
['id': 'id3', 'date': '2020-05-12', 'time': '03:04:44.000'],
['id': 'id7', 'date': '2020-08-17', 'time': '05:02:54.000'],
['id': 'id4', 'date': '2020-09-23', 'time': '07:16:23.000']