« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/data/composite/data/withSortedList.js17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js
index a7942ab4..3a069cf7 100644
--- a/src/data/composite/data/withSortedList.js
+++ b/src/data/composite/data/withSortedList.js
@@ -60,10 +60,14 @@ export default templateCompositeFrom({
         const equalSymbols =
           new Map();
 
-        const indexMap =
+        const symbolToIndex =
           new Map(Array.from(symbols,
             (symbol, index) => [symbol, index]));
 
+        const indexToSymbol =
+          new Map(Array.from(symbols,
+            (symbol, index) => [index, symbol]));
+
         const assertEqual = (symbol1, symbol2) => {
           if (equalSymbols.has(symbol1)) {
             equalSymbols.get(symbol1).add(symbol2);
@@ -75,8 +79,8 @@ export default templateCompositeFrom({
         symbols.sort((symbol1, symbol2) => {
           const comparison =
             sortFn(
-              list[indexMap.get(symbol1)],
-              list[indexMap.get(symbol2)]);
+              list[symbolToIndex.get(symbol1)],
+              list[symbolToIndex.get(symbol2)]);
 
           if (comparison === 0) {
             assertEqual(symbol1, symbol2);
@@ -87,7 +91,7 @@ export default templateCompositeFrom({
         });
 
         const sortIndices =
-          symbols.map(symbol => indexMap.get(symbol));
+          symbols.map(symbol => symbolToIndex.get(symbol));
 
         const sortedList =
           sortIndices.map(index => list[index]);
@@ -118,7 +122,10 @@ export default templateCompositeFrom({
             }, [0]);
 
         const unstableSortIndices =
-          sortIndices.map(stable => stableToUnstable[stable]);
+          Array.from(
+            {length: list.length},
+            (_, index) =>
+              stableToUnstable[symbols.indexOf(indexToSymbol.get(index))]);
 
         return continuation({
           ['#sortedList']: sortedList,