Javascript array compare

Date: 2021-02-10
function arrayCompare(a, b, fn) {
    let i = 0;
    while (i < a.length) {
        const r = fn(a[i], b[i]);
        if (r !== 0) return r;
        i++;
    }
    return 0;
};

// example:
[
        "/js/app-cc925cd038.js",
        "/fonts/glyphicons-halflings-regular.ttf",
        "/",
        "/assets/locale/nl-nl.json",
        "/fonts/glyphicons-halflings-regular.woff",
        "/fonts/fontawesome-webfont.svg",
        "/fonts/glyphicons-halflings-regular.eot",
        "/fonts/fontawesome-webfont.woff2",
        "/assets/locale/en-us.json",
        "/styles/lib-97fce33880.css",
        "/assets/locale/cultures.json",
        "/index.html",
        "/fonts/fontawesome-webfont.ttf",
        "/fonts/fontawesome-webfont.eot",
        "/styles/app-22ee7654ac.css",
        "/fonts/FontAwesome.otf",
        "/js/lib-0fe6c755ea.js",
        "/fonts/glyphicons-halflings-regular.svg",
        "/serviceWorker.js",
        "/fonts/fontawesome-webfont.woff",
        "/fonts/glyphicons-halflings-regular.woff2",
].sort((a, b) => arrayCompare(a.split("/"), b.split("/"), (a, b) => String(a).localeCompare(b)));

/*
[
  "/",
  "/assets/locale/cultures.json",
  "/assets/locale/en-us.json",
  "/assets/locale/nl-nl.json",
  "/fonts/fontawesome-webfont.eot",
  "/fonts/fontawesome-webfont.svg",
  "/fonts/fontawesome-webfont.ttf",
  "/fonts/fontawesome-webfont.woff",
  "/fonts/fontawesome-webfont.woff2",
  "/fonts/FontAwesome.otf",
  "/fonts/glyphicons-halflings-regular.eot",
  "/fonts/glyphicons-halflings-regular.svg",
  "/fonts/glyphicons-halflings-regular.ttf",
  "/fonts/glyphicons-halflings-regular.woff",
  "/fonts/glyphicons-halflings-regular.woff2",
  "/index.html",
  "/js/app-cc925cd038.js",
  "/js/lib-0fe6c755ea.js",
  "/serviceWorker.js",
  "/styles/app-22ee7654ac.css",
  "/styles/lib-97fce33880.css"
]
*/

46090cookie-checkJavascript array compare