In my Angular application, I am trying to sort two arrays of different objects by date. The first object has properties appointmentId and appointmentDate, while the second object has properties cancelId and cancelDate. Currently, my code sorts the items by the appointment date only. How can I modify it to also sort by the cancellation date so that I have a combined sorted list?
let appointments = this.appointmentService.getAppointmentsForJob(this.jobId);
let cancellations = this.cancellationService.getCancellationsForJob(this.jobId);
forkJoin([appointments, cancellations]).subscribe(results => {
this.appointments = results[0];
this.cancellations = results[1];
this.loading = false;
// Combine both arrays and sort by appointment date
this.combinedItems = ([]).concat(this.appointments, this.cancellations);
this.combinedItems = this.combinedItems.sort((a, b) => new Date(a.appointmentDate) - new Date(b.appointmentDate));
console.log(this.combinedItems);
});