I want to sort an array of days, I found using a pre-defined array reference is better than the JS Date class method.
My array on which sort is called upon doesn’t get sorted, even if I always return 1
in the callback, the array never changes.
const days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
const toSort = ["monday", "tuesday", "thursday", "friday", "wednesday"];
toSort.sort((a, b) => {
a = days.indexOf(a);
b = days.indexOf(b);
return a < b ? 0 : 1;
// No matter what I return, the source array remains unchanged
return a < b ? 1 : 0;
return 1;
});
console.log(toSort);
Something so out of order (haha get it) from using a basic JS function must mean the answer is stupidly obvious, but I can’t figure it out.