Stuck with one problem. I have to sort my array of objects by some values. For example by department name -> department.name
. But sometimes department
will have no any keys department: undefined
. So when i will use department.name
i will get errors.
Questions is: Is it possible to sort array with this criteria? And how to modify my function to do it right way?
Thanks!
My function
sort(type) {
const isSorted = this.state.sorted[type];
let direction = isSorted ? 1 : -1;
const sorted = [].slice.call(employees).sort((a, b) => {
if (a.department[type] === b.department[type]) { return 0; }
return a.department[type] > b.department[type] ? direction : direction * -1;
}
}