{"id":5325,"date":"2021-09-21T10:08:52","date_gmt":"2021-09-21T09:08:52","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5325"},"modified":"2022-07-25T08:35:41","modified_gmt":"2022-07-25T07:35:41","slug":"typescript-javascript-orderby","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-javascript-orderby\/","title":{"rendered":"Typescript \/ javascript OrderBy"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const isSet = (x: any) => x != null; \/\/ works for [null, undefined], [ false, 0, Nan ] are true\nconst isNumber = (x: any) => typeof x === \"number\";\nconst isString = (x: any) => typeof x === \"string\";\nconst isIterable = (value: any) => Symbol.iterator in Object(value);\n\nconst stringComparer = (locales?: any, options?: any) => {\n    if (!locales) locales = \"en\";\n    if (!options) options = { numeric: true, sensitivity: \"base\" };\n    const collator = new Intl.Collator(locales, options);\n    return (a: string, b: string) => collator.compare(a, b);\n};\nconst comparer = stringComparer();\n\nfunction compare(a: any, b: any, reverse: boolean): number {\n    const [x, y] = reverse ? [b, a] : [a, b];\n    if (!isSet(x) &amp;&amp; !isSet(y)) return 0;\n    if (!isSet(x) &amp;&amp; isSet(y)) return -1;\n    if (!isSet(y) &amp;&amp; isSet(x)) return 1;\n\n    if (isNumber(x) &amp;&amp; isNumber(y)) return x - y;\n    if (isString(x) &amp;&amp; isString(y)) return comparer(x, y);\n\n    if (isIterable(x) &amp;&amp; isIterable(y)) {\n        for (const d of Array.from(zipIterables(x, y))) {\n            const c = compare(d[0], d[1], reverse);\n            if (c !== 0) return c;\n        }\n        return 0;\n    }\n    return comparer(String(x), String(y));\n}\n\nexport function orderBy&lt;T>(array: T[], fn: (x: T) => any, reverse?: boolean) {\n    const result = Array.from(array);\n    result.sort((a, b) => compare(fn(a), fn(b), reverse || false));\n    return result;\n};\n\n\/\/ Example use:\n\/\/ Sort events first on 'start' date property, when start equals compare the 'end' property\norderBy(events, (e) => [e.start, e.end]);\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-5325","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\/5325","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=5325"}],"version-history":[{"count":9,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5325\/revisions"}],"predecessor-version":[{"id":5394,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5325\/revisions\/5394"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}