{"id":3931,"date":"2020-08-21T10:36:24","date_gmt":"2020-08-21T09:36:24","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3931"},"modified":"2022-07-25T08:35:42","modified_gmt":"2022-07-25T07:35:42","slug":"javascript-string-compare","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-string-compare\/","title":{"rendered":"Javascript string compare"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(() => {\n    const stringComparer = (locales, options) => {\n        if (!locales) locales = 'en';\n        if (!options) options = { numeric: true, sensitivity: 'base' };\n        const collator = new Intl.Collator(locales, options);\n        return (a, b) => () => collator.compare(a, b);\n    };\n    const thenBy = (...args) => {\n        for (const arg of args) {\n            const x = arg();\n            if (x !== 0)\n                return x;\n        }\n        return 0;\n    };\n    const compareStr = stringComparer();\n    const data = [{ firstName: 'a', lastName: 'b' }, { firstName: 'c', lastName: 'a' }, { firstName: 'b', lastName: 'a' }];\n    data.sort((a, b) => thenBy(compareStr(a.lastName, b.lastName), compareStr(a.firstName, b.firstName)));\n    console.log(data);\n})();<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4,1],"tags":[],"class_list":["post-3931","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3931"}],"version-history":[{"count":10,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3931\/revisions"}],"predecessor-version":[{"id":3942,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3931\/revisions\/3942"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}