« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/wiki-data.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/wiki-data.js')
-rw-r--r--src/util/wiki-data.js25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js
index 3e564b9..97ffab7 100644
--- a/src/util/wiki-data.js
+++ b/src/util/wiki-data.js
@@ -152,14 +152,25 @@ export function sortByDirectory(
 }
 
 export function sortByName(data, {getName = (o) => o.name} = {}) {
+  const nameMap = new Map();
+  const normalizedNameMap = new Map();
+  for (const o of data) {
+    const name = getName(o);
+    const normalizedName = normalizeName(name);
+    nameMap.set(o, name);
+    normalizedNameMap.set(o, normalizedName);
+  }
+
   return data.sort((a, b) => {
-    const an = getName(a);
-    const bn = getName(b);
-    const ann = normalizeName(an);
-    const bnn = normalizeName(bn);
-    return (
-      compareCaseLessSensitive(ann, bnn) || compareCaseLessSensitive(an, bn)
-    );
+    const ann = normalizedNameMap.get(a);
+    const bnn = normalizedNameMap.get(b);
+    const comparison = compareCaseLessSensitive(ann, bnn);
+    if (comparison !== 0)
+      return comparison;
+
+    const an = nameMap.get(a);
+    const bn = nameMap.get(b);
+    return compareCaseLessSensitive(an, bn);
   });
 }