.sort((a, b) => a.position - b.position). У какого то элемента не будет position. Он вставился на то место где и был. Это будет везде работать или где-то возможна некорректное поведение?
A negative value indicates that a should come before b.
A positive value indicates that a should come after b.
Zero or NaN indicates that a and b are considered equal.
More formally, the comparator is expected to have the following properties, in order to ensure proper sort behavior:
Pure: The comparator does not mutate the objects being compared or any external state. (This is important because there's no guarantee when and how the comparator will be called, so any particular call should not produce visible effects to the outside.)
Stable: The comparator returns the same result with the same pair of input.
Reflexive: compareFn(a, a) === 0.
Anti-symmetric: compareFn(a, b) and compareFn(b, a) must both be 0 or have opposite signs.
Transitive: If compareFn(a, b) and compareFn(b, c) are both positive, zero, or negative, then compareFn(a, c) has the same positivity as the previous two.
const arrWithPos = arr.filter(x => x.position != null);
arrWithPos.sort((a, b) => a.position - b.position);
let p = 0;
for (let i = 0; i < arr.length; ++i) {
if (arr[i].position != null) {
arr[i] = arrWithPos[p];
p++;
}
}