Javascript string compare

Date: 2020-08-21
(() => {
    const stringComparer = (locales, options) => {
        if (!locales) locales = 'en';
        if (!options) options = { numeric: true, sensitivity: 'base' };
        const collator = new Intl.Collator(locales, options);
        return (a, b) => () => collator.compare(a, b);
    };
    const thenBy = (...args) => {
        for (const arg of args) {
            const x = arg();
            if (x !== 0)
                return x;
        }
        return 0;
    };
    const compareStr = stringComparer();
    const data = [{ firstName: 'a', lastName: 'b' }, { firstName: 'c', lastName: 'a' }, { firstName: 'b', lastName: 'a' }];
    data.sort((a, b) => thenBy(compareStr(a.lastName, b.lastName), compareStr(a.firstName, b.firstName)));
    console.log(data);
})();
39310cookie-checkJavascript string compare