JSFiddle - React, Tailwind, and code Playground

by Graham Fairweather

JavaScript

function arrayMove(array, from, to) {
    return array.splice(to, 0, array.splice(from, 1)[0]);
}

// return an array of moved coordinates
function minMoves(arr1, arr2) {
	// take a shallow copy of arr2 so that the original is not modified
	arr2 = arr2.slice();

	// apply a function against an accumulator (moves) for each value of
	// the array (arr1) (from left-to-right)
	return arr1.reduce(function (moves, item, index) {
        var last;
        
		// if the values of each array at the index are not the same
		if (item !== arr2[index]) {
			// swap the current indexed element of arr2 with the value of
			// the correct element as indexed in arr1. Add the moved
			// coordinates to the beginning of the accumulator
            last = arr2.lastIndexOf(item);
            arrayMove(arr2, last, index);
			moves.unshift([index, last]);
		}

		// return the accumulater for the next iteration
		return moves;
	}, []);
}

var before = [1, 5, 6, 3, 2, 4, 7, 8, 9, 0],
    test = before.slice(),
    after  = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
    moves = minMoves(before, after);

console.log('moves: ' + JSON.stringify(moves));
moves.forEach(function(move) {
	arrayMove(test, move[0], move[1]);
});

console.log('Should be ordered nicely: ' + JSON.stringify(test));