Maybe I'm not asking this question properly, but I can't seem to find the solution online.
I have an array of employees that I need to sort by different criteria like position or department.
There are several employees with similar values (such as multiple bartenders or guest services staff).
So, what I need is to group these employees (all bartenders or all guest service staff) and then sort them alphabetically by last name, and if needed, further sort them by first name within the same last name.
This is how my current sorting function looks:
const sorted = state.employees.sort(function(a, b) {
if (a[filterValue] === b[filterValue]) {
let alphaA = a.lastName, alphaB = b.lastName;
return alphaA < alphaB ? -1 : alphaA > alphaB ? 1 : 0;
}
return a[filterValue] > b[filterValue] ? 1 : -1;
});
However, when I click on the sort functionality, the order changes each time, as shown in this GIF: https://i.sstatic.net/V3Pzv.gif.
Also, there isn't any logic for ascending/descending order, so clicking multiple times should ideally give the same sorted array instead of different orders every time.
I suspect the issue might be with how the alphabetical comparison is handling the matching values within the same subgroup (like all employees in accommodations department), but I'm unsure and need help resolving it.
I would appreciate assistance in completing this task, along with an explanation as I'm still trying to grasp how sorting functions work.
Thank you!