Why isn't my array sorting in my typescript code (angular component) after being populated? When I manually populate the array it sorts correctly. What could be causing this issue?
Example of working code:
let chartArr: any = [];
chartArr = [
{text: 'AA', assignedTasks: 3},
{text: 'BB', assignedTasks: 7},
{text: 'CC', assignedTasks: 1},
{text: 'DD', assignedTasks: 10},
];
console.log(this.chartArr.sort((a, b) => b.assignedTasks - a.assignedTasks ));
Example of code that doesn't work:
let chartArr: any = [];
doc.forEach(user => {
eventTasksRefCount = this.afs.collection('eventTask', ref => ref.where('uid', '==', user.uid));
eventTasksObservableCount = eventTasksRefCount.valueChanges();
eventTasksObservableCount.subscribe(result => {
this.chartArr.push({text: user.displayName, assignedTasks: result.length });
});
console.log(this.chartArr.sort((a, b) => b.assignedTasks - a.assignedTasks ));