« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
Diffstat (limited to 'src/data')
-rw-r--r--src/data/composite/data/withSortedList.js19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js
index 9ee0766..eda050a 100644
--- a/src/data/composite/data/withSortedList.js
+++ b/src/data/composite/data/withSortedList.js
@@ -95,11 +95,16 @@ export default templateCompositeFrom({
           return comparison;
         });
 
-        const sortIndices =
-          symbols.map(symbol => symbolToIndex.get(symbol));
-
-        const sortedList =
-          sortIndices.map(index => list[index]);
+        const symbolToStable = new Map();
+        const stableSortIndices = [];
+        const sortedList = [];
+
+        for (const [stableIndex, symbol] of symbols.entries()) {
+          const sourceIndex = symbolToIndex.get(symbol);
+          stableSortIndices.push(sourceIndex);
+          symbolToStable.set(symbol, stableIndex);
+          sortedList.push(list[sourceIndex]);
+        }
 
         const push = (array, value) => {
           array.push(value);
@@ -119,12 +124,12 @@ export default templateCompositeFrom({
         const unstableSortIndices =
           originalIndices
             .map(index => indexToSymbol.get(index))
-            .map(symbol => symbols.indexOf(symbol))
+            .map(symbol => symbolToStable.get(symbol))
             .map(stable => stableToUnstable[stable]);
 
         return continuation({
           ['#sortedList']: sortedList,
-          ['#sortIndices']: sortIndices,
+          ['#sortIndices']: stableSortIndices,
           ['#unstableSortIndices']: unstableSortIndices,
         });
       },