I have a JavaScript Array called years
with some dates and one textual item like so: [ "2017", "2018", "2019", "historic" ]
I want to sort it so that 'historic' comes first, and then the remaining numbers are sorted numerically.
The following sort function works perfectly in every browser I have tested apart from Firefox, which leaves 'historic' at the end of the array:
years.sort((a, b) => {
if (a === 'historic') {
return -1;
} else {
return a - b;
}
});
I tried adding the following console log:
let years = [ "2017", "2018", "2019", "historic" ];
years.sort((a, b) => {
console.log(a);
if (a === 'historic') {
return -1;
} else {
return a - b;
}
});
Firefox outputs the following:
2017 index.js:409
2018 index.js:409
2019
Suggesting it never processes the 'historic' array element.