From 763b0b7c3c4c98b77f451d07ee3fbd060cf87550 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Sun, 23 Aug 2020 18:14:51 +0200 Subject: [PATCH] Refactor sortMoveOperations a bit --- src/logic/sort.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/logic/sort.js b/src/logic/sort.js index 569413c..ceb7e03 100644 --- a/src/logic/sort.js +++ b/src/logic/sort.js @@ -66,15 +66,15 @@ export function sortObjectKeys (object, direction = 1) { */ export function sortMoveOperations (array, comparator) { const operations = [] + const sorted = [] - const sorted = array.slice() - - for (let i = 1; i < sorted.length; i++) { + for (let i = 0; i < array.length; i++) { // TODO: implement a faster way to sort (binary tree sort?) // TODO: can we simplify the following code? - if (comparator(sorted[i - 1], sorted[i]) > 0) { + const item = array[i] + if (i > 0 && comparator(sorted[i - 1], item) > 0) { let j = i - 1 - while (comparator(sorted[j - 1], sorted[i]) > 0 && j > 0) { + while (j > 0 && comparator(sorted[j - 1], item) > 0) { j-- } @@ -83,8 +83,9 @@ export function sortMoveOperations (array, comparator) { toIndex: j }) - const item = sorted.splice(i, 1) sorted.splice(j, 0, [item]) + } else { + sorted.push(item) } }