Q:

Array sort with mixed alphabetical and numerical content not working in Firefox

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.

A:

You need a symetrically check for both elements and check if a value is 'historic'. Then return the delta of the check or the delta of the values.

var array = ["2017", "2018", "2019", "historic"];

array.sort((a, b) => (b === 'historic') - (a === 'historic') || a - b);

console.log(array);

