I tried this method:
if (field == 'age') {
if (this.sortedAge) {
this.fltUsers.sort(function (a, b) {
if (b.totalHours > a.totalHours) {
return 1;
}
});
this.sortedAge = false;
} else {
this.sortedAge = true;
this.fltUsers.sort(function (a, b) {
if (b.totalHours < a.totalHours) {
return 1;
}
});
}
}
So I have array of objects. Each object has property: totalHours.
I need to order this array by desc/asc this field totalHours after click.
My code does not work.
1,-1or0depending on the comparison.this.fltUsers.sort((a, b) => a.totalHours - b.totalHours)